- Bubble sort - n-1 passes, each compares each pair of adjacent elements and swaps if ooo
iterate n-1 times {
for the ith element (i ranging from 0 to n-1) {
if (out of order (relative to the next element)
swap ith and i+1rst values, ith and next value?
}
}
- Selection sort - n-1 passes; the ith finds the smallest remaining (i.e. at i or more) element and swaps it to position i
iterate n-1 times (i ranging from 0 to n-1) {
swap ith and indexOfSmallest from i to n-1
}
}
In more detail:
iterate n-1 times (for i=0 to n-1) {
indexOfSmallest = findSmallestFromIthDown(i)
swap ith and indexOfSmallest from i to n-1
}
}
int findSmallestFromIthDown(int i) {
...
}
- Insertion sort - repeatedly insert one more element into a sorted list
iterate n-1 times (i ranging from 1 to n-1) {
for the ith element {
insert it where it goes in the sorted list above it (must shift down the list first)
}
}
In more detail:
iterate n-1 times (i=1 to n-1) {
insertHere = findWhereItGoesAboveIth(i)
pocket = list[i] // so it doesn't get shifted onto!
shiftDown(insertHere, i)
list[insertHere] = pocket
}
- Cleaning up the examplesFromClass code.