Bubble Sort

Roshan Kumar Sah
Dev Genius
Published in
2 min readApr 1, 2021

--

Definition:
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements “bubble” to the top of the list.

Bubble Sort:

var unOrderArray = [5,2,13,7,11,3]
Suppose we have array of unordered numbers(In this case Prime number, subset of natural numbers)
We have two option to sort this.
1. Ascending order.
2. Descending order.
Bubble Sort (Ascending Order)

But what if all elements are sorted itself.
ex: var sortedArray = [2, 3, 5, 7, 11, 13]
It will sort all sorted elements 😂.
This case can be handled by assuming all element are sorted and if it holds true than we will break the loop.✌🏼

Bubble Sort (Ascending Order)

Supporting code:

Thanks for reading.
Please let me know if you have any queries or doubt.

roshankumar350@gmail.com
Roshan kumar Sah.

--

--