Example for Bubble Sort
So we take an array with 5 elements (-3, 15, 0, 8, -7) and we have to sort elements in Ascending Order.
First Iteration:
- Starting from the first index, compare the first and the second element.
- If it comes out that the first element is greater than the second element, then they both are swapped.
- Now, compare the second and the third elements. If is comes out that they are not in order then swap them.
- The above process continues till the last element.
.png)
Remaining Iteration
- So, we perform the same process for the remaining iterations.
- After eeach iteration, the largest element among the unsorted elements is placed at the end of the array.
.png)
In each iteration the comparison continues up to the last unsorted element.
.png)
When all the unsorted elements are placed at their correct positions, the array becomes sorted.
.png)
8
