BUBBLE SORT

Bubble sorting is a basic algorithm for sorting. This sorting algorithm is a comparison-based algorithm that compares each pair of adjacent elements and, if they are not in order, swaps the elements. For large data sets, this algorithm is not suitable because its average and worst-case complexity is Ο(n2), where n is the number of products.

Working

We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we’re keeping it short and precise.

The sorting begins with the first two components, comparing them to verify which one is greater.Bubble sort starts with very first two elements, comparing them to check which one is greater.

Value 33 is greater than 14 in this situation, because it is already in the sorted positions. Next, we will equate 33 to 27.

We notice that 27 is less than 33 and it is important to swap these two values..


The new array should look like this −

We’ll compare 33 and 35 next. We notice that both are in positions which are already sorted.

Then we move to the next two values, 35 and 10.

Therefore, we know that 10 is less than 35. They are also not sorted.



We’re exchanging these values. We notice that the end of the array has reached us. The array should look like this after one iteration,

To be accurate, after every iteration, we are now showing how an array should look. It should look like this after the second iteration,

Notice that after each iteration, at least one value moves at the end.

And bubble sorts learn that an array is fully sorted when there is no swap needed.

Algorithm

We assume that the list is an array of elements with n. Furthermore, we assume that the swap function swaps the values of the array elements given.

Pseudocode