The bubble sort is a slow but simple method of sorting elements in a one-dimensional array.
It works as follows,
Take the following array of numbers,
2, 23, 9, 79, 31, 81, 53, 13, 33, 43
On the first pass, some items are swapped, leaving the list as follows,
2, 9, 23, 31, 79, 53, 13, 33, 43, 81
The process is repeated and further swaps are made,
2, 9, 23, 31, 53, 13, 33, 43, 79, 81
Third pass,
2, 9, 23, 31, 13, 33, 43, 53, 79, 81
Fourth pass,
2, 9, 23, 13, 31, 33, 43, 53, 79, 81
Fifth pass,
2, 9, 13, 23, 31, 33, 43, 53, 79, 81
We made some swaps on the last pass, so we still repeat the process. No swaps are made on the sixth pass, so the list is in order,
2, 9, 13, 23, 31, 33, 43, 53, 79, 81
Repeat
swapflag = false
For count = 1 To N - 1
If bubbleItem[count]>bubbleItem[count + 1] Then
temp = bubbleItem[count]
bubbleItem[count] = bubbleItem[count + 1]
bubbleItem[count + 1] = temp
swapflag = true
End If
Next
N = N - 1
Until swapflag = false Or N = 1