Computer Applications
Answer
Bubble Sort is a sorting algorithm that works by repeatedly iterating through the array, comparing each pair of adjoining elements and swapping them if they are in wrong order.
For example, consider the following unsorted array:
9 | 5 | 2 | 3 |
Pass 1
First 9 is compared with 5 and as 9 is greater than 5 the elements are swapped:
5 | 9 | 2 | 3 |
Next, 9 is compared with 2 and as 9 is greater than 2 the elements are swapped:
5 | 2 | 9 | 3 |
Next, 9 is compared with 3 and as 9 is greater than 3 the elements are swapped:
5 | 2 | 3 | 9 |
At the end of first pass, the highest element of the array is at the last position.
Pass 2
5 is compared with 2 and as 5 is greater than 2 the elements are swapped:
2 | 5 | 3 | 9 |
Next, 5 is compared with 3 and as 5 is greater than 3 the elements are swapped:
2 | 3 | 5 | 9 |
At the end of first pass, the second highest element of the array is in its correct position.
Pass 3
2 is compared with 3 and as 2 is less then 3 no swapping takes place.
2 | 3 | 5 | 9 |
With this, the third and final pass ends and the elements of the array are in sorted order.