How to write a bubble sort in pseudocode?
array myNumbers[6]
for (i = 0 to myNumbers.length - 2)
swapped = false
for (j = 1 to myNumbers.length - 1 - i)
if (myNumbers[j-1] > myNumbers[j])
temp = myNumbers[j-1]
myNumbers[j-1] = myNumbers[j]
myNumbers[j] = temp
swapped = true
endif
next j
if (swapped == false) then
break //breaks out of loop
endif
next i
Describe how a bubble sort works.
Bubble sort repeatedly passes through the items to be sorted. On each pass adjacent items are compared and swap positions if the one on the left is larger than the one on the right. This continues to the end of the array. At this point the first pass is complete. If there have been swaps, another pass is made. The passes continue until no swaps are made and the data is sorted.