SAM Sort
( Slow And Meticulous Sort )
How SAM sort works:
1. Compare the first value to the last value in the set
2. If the first value is larger than the last value, swap the values
3. Continue with each value in set, starting with the next one and up through the [length-1] value, repeating the swapping step
4. After the first pass, the last number in sequence should be the largest
5. The algorithm then goes back to the first value and compares the first value to the [length–1] value in set
6. If first value larger than the [length–1] value, swap the values
7. Continue with each value in set, starting with the next one and up through the [length-2] value, repeating the swapping step
8. After the second step, repeat with first value and compare with [length – 2]
9. Do this with all values, continuing to compare the first value to the last unsorted value, until eventually comparing the first with second value
10. Swap these last two if needed, check and finish
So how is this different than bubble sort?
SAM sort can be thought of in a very similar way to bubble sort. The main difference comes from the swapping. SAM sort swaps the [i] value with [length-i]
value, whereas bubble sort swaps the [i] value with [i+1].
How efficient is it?
This sorting algorithm has an average complexity of O(n^2). Meaning it has the same complexity as insertion sort or bubble sort.
Time comparison of the three below.
Sorting n=100 at tickspeed=100
Insertion Sort: 00:00:23.29(23 seconds)
Bubble Sort: 00:00:25.04 (25 seconds)
SAM Sort: 00:00:51.68 (51 seconds)
Is there a way to test it?
In order to create and verify my algorithm, I used two different methods to test it. After writing down the process on paper, I coded the sort in javascript
(code below). After running the program many times with successful results, I created a sorting algorithm visualizer in Python using Pygame. I used this
visualizer with my sorting algorithm to make sure each step of the sort was working properly.
Javascript.[EXPAND]
sequence = [1,63,68,33,28,40,10,70,57,55];
i = 0;
j = sequence.length - 1;
k = 0;
let temp;
while (k < sequence.length){
// checks each i with last digit
while (i < sequence.length - 1 - k) {
if (sequence[i] > sequence[j]) {
temp = sequence[j];
sequence[j] = sequence[i];
sequence[i] = temp;
i++;
}
else {
i++
}
}
i = 0;
j--;
k++;
console.log(sequence);
}
Python.[EXPAND]
while k < len(lst):
while i < (len(lst) - 1 - k):
if (lst[i] > lst[j] and ascending) or (lst[i] < lst[j] and not ascending):
# swap both
lst[i], lst[j] = lst[j], lst[i]
i += 1
else:
i += 1
i = 0
j -= 1
k += 1
return lst
}
Sorting Algorithm Visualizer.[EXPAND]
Download the Sorting Algorithm Visualizer Python program to test for yourself. Change the amount of values
to be sorted, the speed of which it is sorted, if the sorting method is ascending or descending, and which
sorting method to use. Currently, the only sorting algorithms programmed in are Bubble sort, Insertion sort, and
SAM sort.
- Requires Pygame to be installed.
- Download link not available on mobile.