> They are, in reality, minor tweaks or variation on bubble sort
I don't understand how you reach such conclusion
it's like voodoo programming, change a sign here and there, ¡behold, a new sorting algorithm!
sure, the code does look similar, but the underlying ideas, the mechanisms of why they work, are totally unrelated
> For example (from memory) selection skips extra work (that bubble does) if
> its in reversed order
consider this pseudocode (the subarray is not copied, len() is O(1))
1 2 3 4 5 6 7 8 9 10 11 12
|
def selection_sort(array):
for K in range(len(array)-1):
min_index = min_element(array[K:])
if K not_eq min_index:
swap(array[K], array[min_index])
def min_element(array):
min_index = 0
for K in range(1, len(array)):
if array[K] < array[min_index]:
min_index = K
return min_index
|
regardless of the start order of the array, the algorithm always does the same number of comparisons
however, at worst it will do n-1 swaps, which is good if the objects are big
so it's an indirect sort that only requires O(1) extra memory
another good thing is within the min_element() function
there are better implementations to extract the minimum element from a set, like a binary search tree (treesort) or a priority queue (heapsort) which gives you O(n lg n)
kind of hate the fascination with teaching bubble-sort, the algorithm is bad, is not quite clear how it works (¿how many iterations? ¿does it stop?), it has an embed (not visible) is_sorted() function and the bubbles are rocks.