Gnome Sort is brand new to the family.

The girlfriend of Pigeonhole Sort and a hopeless romantic, she is eager to meet his parents and siblings and make good impressions on them. She can’t wait for a chance to show off the bounty of her vegetable garden, and has brought squash, carrots, and fresh spinach to the reunion in the hopes of impressing her (maybe, potentially, hopefully) soon-to-be in-laws.

Photo by Kenan Kitchen / Unsplash

She might be out of luck, though. Pigeon has done this before, and the girlfriends he brings to the reunion aren’t seen at the next one. Nevertheless, Gnome Sort hopes her gifts of food and joy help tell Pigeon’s family that maybe she really is the real deal.

Even if she isn’t, the family figures, they still get free fresh veggies. That’s a good deal.

The Rundown

Algorithm

Get “The Catch Block”, My Weekly Newsletter!

Plus my eBook “The Daily Design Pattern”, early access, new posts in your inbox, and more for becoming a paid subscriber.

Go!

I will not spam, sell your email, etc.

  1. START at the beginning of the array.
  2. IF the pot at the current position and next-highest pot are in the correct order, go to next position.
  3. ELSE if they are not in order, SWAP them.
  4. GO TO 2, CONTINUE UNTIL all pots are sorted.

Visualization

The nice thing about this visualization is that it makes it obvious what the “gnome” is doing. It also makes it very clear that this is a terribly inefficient algorithm.

The part that the visualization is skipping is that when a number is place into it’s final sorted position, the “gnome” needs to step back up the sorted items to find the next unsorted item. Even if it does this quickly, it’s doing many more accesses than other algorithms.

This one also has an “audibilization”; it sounded kind of like a police siren on speed to me. What do you think?

Implementation

class GnomeSort
{

    static void Sort(int[] arr, int length)
    {
        int index = 0;

        while (index < length)//If there is no pot next to the gnome, he is done.
        {
            if (index == 0) //If the gnome is at the start of the line...
            {
                index++;//he steps forward
            }

            if (arr[index] >= arr[index - 1])//If the pots next to the gnome are in the correct order...
            {
                index++;//he goes to the next pot
            }
            else //If the pots are in the wrong order, he switches them.
            {
                int temp = 0;
                temp = arr[index];
                arr[index] = arr[index - 1];
                arr[index - 1] = temp;
                index--;
            }
        }
        return;
    }

    public static void Main()
    {
        int[] array = { 84, 61, 15, 2, 7, 55, 19, 40, 78, 33 };

        CommonFunctions.PrintInitial(array);

        Sort(array, array.Length);

        CommonFunctions.PrintFinal(array);

        Console.ReadKey();
    }
}

#exchange sorts #algorithms

Gnome Sort - The Sorting Algorithm Family Reunion
1.10 GEEK