SAM Sort
( Slow And Meticulous Sort )
Why did I make it? There was no practical reason for the development of this sorting algorithm method. After learning about different types of sorting algorithms in class, I became interested in how they work. That led to the question of whether or not I could make my own. I assumed that it would be possible, although to create a sorting algorithm that is more efficient than existing ones would be nearly impossible. That being said, I am still looking into that possibility and if I can achieve a new or alternative sorting method which will be just as, if not more, efficient than the current methods.

How did I come up with SAM sort? As I said, coming up with a newly efficient sorting algorithm would be extremely difficult, if it is even possible. So I decided to create a sorting algorithm that isn't necessarily quick, but still works... Taking inspiration from the initials of my name (duh), I decided to call it Slow and Meticulous, but there are still a handful of algorithms which sort even slower than mine. After having a name in mind, I needed to research other algorithms and see what worked, and what didn't. Next, it was time to create a process and establish rules for how it would perform.
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.