Bubble Sort Algorithm is used to arrange N elements in ascending order, and for that, you have to begin with 0th element and compare it with the first element. If the 0th element is found greater than the 1st element, then the swapping operation will be performed, i.e., the two values will get interchanged. In this way, all the elements of the array get compared.
Below given figure shows how Bubble Sort works:
Algorithm for Bubble Sort
- algorithm Bubble_Sort(list)
- Pre: list != fi
- Post: list is sorted in ascending order for all values
- for i <- 0 to list:Count - 1
- for j <- 0 to list:Count - 1
- if list[i] < list[j]
- Swap(list[i]; list[j])
- end if
- end for
- end for
- return list
- end Bubble_Sort