4 ms·
Now I get to be that guy. Your i only needs to go to n-1, and your j can start with i+1. More importantly, what you wrote is a selection sort, not a bubble sor
by eesmith 7y ago
Now I get to be that guy. Your i only needs to go to n-1, and your j can start with i+1.
More importantly, what you wrote is a selection sort, not a bubble sort!
A bubble sort swaps neighboring terms. Eg, if I interpret the code in 2.1 of https://users.cs.duke.edu/~ola/bubble/bubble.html https://users.cs.duke.edu/~ola/bubble/bubble.html correctly, the 'definitive' version is:
for i = n-1..0:
for j = 0..i:
if arr[j+1] < arr[j]:
arr[j], arr[i] = arr[i], arr[j]
- dickeytk 7y agoFair point. Perhaps it is harder than I thought without looking anything up or testing? At least to get the canonical version written down.
- eesmith 7y agoI think it's that "bubble sort" has come to mean "a simple O(N²) sort implementation", of which there are many. But the selection sort just feels more natural than the actual bubble sort. At the very least, selection sort doesn't get my face to scrunch up in a scowl at all those memory movements.