BUBBLE SORT
Bubble sort is the simplest sorting algorithm and it is based on the idea that every adjacent elements are compared and swapped if found in wrong order.
EXAMPLE:
To understand the bubble sort ,lets consider an unsorted array[1,23,10,-2] and discuss each step taken to sort the array ascending order. In every pass, two adjacent elements are checked and swapped if found in wrong order.
Steps:-
- First pass: (1)and (23) are compared and found in correct order(ascending order in this case). After that (23) and (10) are compared,since (23>10),hence these numbers are swapped. Then (23) and (-2) are compared and swapped.
- Second pass:(1) and (10) are compared and found in correct order .Then(10) and(-2) are compared, since(10>-2), hence these numbers are swapped. After that (10) and (23) are compared and found in correct order.
- Third pass:(1) and (-2) are compared,since(1>-2), hence these numbers are swapped .After that (1,10) and (10,23) are checked and found in correct order.