3 ms·
> Bubble sort, on the other hand, requires you to reason about consecutive adjacent swaps, keep track of whether the current iteration dirtied the array, and so
by tzs 6y ago
> Bubble sort, on the other hand, requires you to reason about consecutive adjacent swaps, keep track of whether the current iteration dirtied the array, and so on.
You only need to keep track of whether or not the current iteration dirtied the array if you want to stop as soon as a pass leaves the array sorted. If you don't want to do that (or don't think of it) then it is just something like:
for pass = 1 to n
for index = 1 to n-1
if array[index-1] > array[index]
tmp = array[index]
array[index-1] = array[index]
array[index] = tmp
There is less extra to keep track of then in insertion or selection sort.