Bubble sort
Given unsorted array is scanned n-1 times(n - number of elements in the array) and always it compares with the adjacent current element and if current element is greater then both elements are swapped.
In the given example array is sorted choosing largest value, and sorted largest to smallest(last element first element).
In the given example array is sorted choosing largest value, and sorted largest to smallest(last element first element).
Comparing 3 and 72 and take 72 as curent element |
Comparing 50 and 72, since 50 is smaller, 50 and 72 are swapped |
Comparing 68 and 72, since 68 is smaller, 68 and 72 are swapped |
Comparing 68 and 72, since 69 is smaller, 69 and 72 are swapped |
Comparing 22 and 72, since 68 is smaller, 22 and 72 are swapped |
72 is sorted to index 5 as the largest value in the array |
Comparing 3 and 50 |
Comparing 50 and 68 |
Comparing 68 and 69 |
Comparing 22 and 69, 69 and 22 are swapped since 22 is smaller |
Array is sorted 4 index to 5 index |
Comparing 3 and 50 |
Comparing 50 and 68 |
Comparing 22 and 68, 22 and 68 areswapped since 22 is smaller |
Last 3 elements in the array are sorted |
Comparing 3 and 50 |
Comparing 22 and 50, 22 and 50 are swapped since 22 is smaller |
Last 4 elements in the array are sorted |
Comparing 3 and 22 |
Last 5 elements are sorted |
Array is sorted |
Comments
Post a Comment