Insertion Sort - The Sorting Algorithm Family Reunion

The eldest child of the family, Insertion Sort has a good life.

Between working as an accountant at a high-powered law firm and her indelible engagement and eventual marriage to loving husband Quick Sort, Insertion Sort (Innie, as she is known to her friends) has a life dreamt of by many. The family relies on her smarts to keep their financial lives balanced, and by all accounts she does an amazing job of this. After all, who’d notice that, occasionally, a very small percentage of it gets, ahem, “redirected” to other causes. The parents don’t even mind all that much.

“Money, if it does not bring you happiness, will at least help you be miserable in comfort.” [1]

Innie can’t help but feel some jealousy toward her sister Heap Sort. She never got Heapy’s culinary skills, and so feels that her parents are slightly disappointed in her, though she knows they would never show it. She is consistently the most well-off member of her family, and she makes sure they know all about it with flashy jewelry and designer clothes and expensive cars, hoping against vain hope that this makes her more “worthy” in her parents’ eyes.

The Rundown

Algorithm

  1. FOR each value in the array
  2. STORE the current value in a variable.
  3. WHILE we are pointing to a valid value…
  4. …IF the current value is less than the value we are point at
  5. …THEN move the pointed-at value up one space, and store the current value at the pointed-at position.

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.

Visualization


You may notice a “stair step” effect in the above visualization; that is, values progressively “stepping” down to the left of the frame. This stair stepping represents the comparison happening during steps 4 and 5 of the algorithm above; Insertion Sort moves each value up one step if it is greater than the current value.

Also, as with the previous entries in this series, check out the very cool “visualization and audibilization” of Insertion Sort from YouTube

Implementation

class InsertionSort
{
    static void Main(string[] args)
    {
        int[] array = new int[15] { 55, 97, 76, 60, 4, 18, 37, 34, 88, 51, 43, 49, 19, 12, 63 };
        Console.WriteLine("Insertion Sort");

        CommonFunctions.PrintInitial(array);

        //1\. For each value in the array...
        for (int i = 1; i < array.Length; ++i)
        {
            //2\. Store the current value in a variable.
            int currentValue = array[i];
            int pointer = i - 1;

            //3\. While we are pointing to a valid value...
            //4\. If the current value is less than the value we are pointing at...
            while (pointer >= 0 && array[pointer] > currentValue)
            {
                //5\. Then move the pointed-at value up one space, and store the
                //   current value at the pointed-at position.
                array[pointer + 1] = array[pointer];
                pointer = pointer - 1;
            }
            array[pointer + 1] = currentValue;
        }

        CommonFunctions.PrintFinal(array);

        Console.ReadLine();
    }
}

Time and Space Complexity

As with most sorting algorithms, the best-case scenario for Insertion Sort is that the array is already sorted; in this case, the algorithm has _O(n) _time, because it still needs to iterate over the entire array even though it won’t do any swaps.

But in the average and worst cases, Insertion Sort doesn’t fare too well with its _O(n2) _complexity. There are a good many sorting algorithms (several of which we will see later in this series) which are substantially more efficient in these situations.

That said, insertion sort has a couple of things going for it, apart from the cushy job and respect from the family. For starters, it’s relatively easy to implement and very efficient on data sets that are already mostly sorted, so in some scenarios it actually performs better than algorithms such as QuickSort. Further, it requires very little addition space to perform the algorithm. In short, it’s a very useful learning algorithm, and has a few practical applications as well.

#insertion sorts #algorithms

What is GEEK

Buddha Community

Insertion Sort - The Sorting Algorithm Family Reunion

Insertion Sort - The Sorting Algorithm Family Reunion

The eldest child of the family, Insertion Sort has a good life.

Between working as an accountant at a high-powered law firm and her indelible engagement and eventual marriage to loving husband Quick Sort, Insertion Sort (Innie, as she is known to her friends) has a life dreamt of by many. The family relies on her smarts to keep their financial lives balanced, and by all accounts she does an amazing job of this. After all, who’d notice that, occasionally, a very small percentage of it gets, ahem, “redirected” to other causes. The parents don’t even mind all that much.

“Money, if it does not bring you happiness, will at least help you be miserable in comfort.” [1]

Innie can’t help but feel some jealousy toward her sister Heap Sort. She never got Heapy’s culinary skills, and so feels that her parents are slightly disappointed in her, though she knows they would never show it. She is consistently the most well-off member of her family, and she makes sure they know all about it with flashy jewelry and designer clothes and expensive cars, hoping against vain hope that this makes her more “worthy” in her parents’ eyes.

The Rundown

Algorithm

  1. FOR each value in the array
  2. STORE the current value in a variable.
  3. WHILE we are pointing to a valid value…
  4. …IF the current value is less than the value we are point at
  5. …THEN move the pointed-at value up one space, and store the current value at the pointed-at position.

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.

Visualization


You may notice a “stair step” effect in the above visualization; that is, values progressively “stepping” down to the left of the frame. This stair stepping represents the comparison happening during steps 4 and 5 of the algorithm above; Insertion Sort moves each value up one step if it is greater than the current value.

Also, as with the previous entries in this series, check out the very cool “visualization and audibilization” of Insertion Sort from YouTube

Implementation

class InsertionSort
{
    static void Main(string[] args)
    {
        int[] array = new int[15] { 55, 97, 76, 60, 4, 18, 37, 34, 88, 51, 43, 49, 19, 12, 63 };
        Console.WriteLine("Insertion Sort");

        CommonFunctions.PrintInitial(array);

        //1\. For each value in the array...
        for (int i = 1; i < array.Length; ++i)
        {
            //2\. Store the current value in a variable.
            int currentValue = array[i];
            int pointer = i - 1;

            //3\. While we are pointing to a valid value...
            //4\. If the current value is less than the value we are pointing at...
            while (pointer >= 0 && array[pointer] > currentValue)
            {
                //5\. Then move the pointed-at value up one space, and store the
                //   current value at the pointed-at position.
                array[pointer + 1] = array[pointer];
                pointer = pointer - 1;
            }
            array[pointer + 1] = currentValue;
        }

        CommonFunctions.PrintFinal(array);

        Console.ReadLine();
    }
}

Time and Space Complexity

As with most sorting algorithms, the best-case scenario for Insertion Sort is that the array is already sorted; in this case, the algorithm has _O(n) _time, because it still needs to iterate over the entire array even though it won’t do any swaps.

But in the average and worst cases, Insertion Sort doesn’t fare too well with its _O(n2) _complexity. There are a good many sorting algorithms (several of which we will see later in this series) which are substantially more efficient in these situations.

That said, insertion sort has a couple of things going for it, apart from the cushy job and respect from the family. For starters, it’s relatively easy to implement and very efficient on data sets that are already mostly sorted, so in some scenarios it actually performs better than algorithms such as QuickSort. Further, it requires very little addition space to perform the algorithm. In short, it’s a very useful learning algorithm, and has a few practical applications as well.

#insertion sorts #algorithms

Alayna  Rippin

Alayna Rippin

1603767600

Search Algorithms

Today, let us touch base on some fundamental concepts like search algorithms.

In simple terms, **searching **is a process of looking up a particular data record in the database or in the collection of items. A search typically answers as true or false whether the particular data in which we are referring is found or not and perform the next action accordingly.

Commonly used algorithms for search are:

  • Linear search
  • Binary search
  • Interpolation search

Let us understand them in detail with some example

Linear Search Algorithm

Linear Search Algorithm is the simplest and easiest form of the search algorithm. In this algorithm, a sequential search is made over all the items one by one to search for the targeted item. Each item is checked in sequence until the match is found. If the match is found, the searched item is returned otherwise the search continues till the end.

To make it easy to understand, let us see understand linear search using a flow diagram

Image for post

Linear Search — Data Flow

Points to note:

  • Does not need sorted array list
  • Performs equality comparisons
  • The time complexity is O(n)
  • Time taken to search elements keeps increasing as the number of elements is increased.

Binary Search Algorithm

In _Binary search algorithm, _begins with an interval covering the whole array and diving it in half. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

To make it easy to understand, let us see understand binary search using flow diagram and example as below.

Image for post

Binary Search — Data Flow

Points to note:

  • The array needs to be sorted
  • Performs ordering comparisons
  • Time complexity to O(log n).
  • Search is done to either half of the given list, thus cut down your search to half time

#sorting-algorithms #algorithms #data-structures #search-and-sort #searching-algorithm

Odd-Even Sort - The Sorting Algorithm Family Reunion EXCHANGE SORTS

Odd-Even Sort is a headstrong little girl.

The middle daughter of Heap Sort and Cocktail Shaker Sort, she has recently discovered she a particular skill, a skill that makes her a nightmare to people like her parents. This skill has allowed her to get extra dessert, more screen time, even less chores; in short, everything a ten-year-old middle child could ever dream of.

That skill? Getting one parent to say yes when the other already said no.

She’s sneaky about it. She will wait minutes, hours even, just to strike the unsuspecting parent at exactly the right moment. She knows just when her opportunity arises: when mom gets home from work, when dad is dealing with her baby brother Bogosort, when her elder sister Counting Sort needs to get to soccer practice. All her life she’s been perfecting this skill, and now she’s a master.

Here’s hoping she uses her powers for good. Or at least extra ice cream.

The Rundown

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.

Algorithm

  1. FOR each item in an odd numbered position
  2. COMPARE that item against the item in the next-highest position.
  3. IF the elements are out of order, SWAP them.
  4. FOR each item in an even-numbered position
  5. COMPARE that item against the item in the next-highest position.
  6. IF the elements are out of order, SWAP them.
  7. GOTO 1, CONTINUE until list is sorted.

Visualization

The base visualization from Wikipedia is cool to look at but doesn’t help me understand this algorithm at all. So let’s demonstrate the algorithm with a simple set of numbers. Here’s our unsorted collection:

{ 15, 62, 42, 29, 17 }

On the first pass of the algorithm, in the first step, we compare the element in position 1 against the element in position 2. Position 1 is 15, position 3 is 62, so these are in order.

The next comparison is between position 3 (42) and position 4 (29). Those elements are out of order, so the algorithm swaps them, resulting in:

{ 15, 62, 29, 42, 17 }

The next move would compare the element in position 5 against the one in position 6, but since there isn’t one in position six, the algorithm instead switches to doing the even-numbered comparison.

The first even comparison (position 2 to position 3) results in:

{ 15, 29, 62, 42, 17 }

The second even comparison results in:

{ 15, 29, 62, 17, 42 }

Now we go back to the odd-numbered comparisons:

{ 15, 29, 17, 62, 42 }

Now back to even-numbered:

{ 15, 17, 29, 62, 42 }

{ 15, 17, 29, 42, 62 }

And now the array is sorted! It took us four passes to do this, and if you’re thinking that seems pretty inefficient, you are not wrong.

#exchange sorts #algorithms

Noah  Rowe

Noah Rowe

1593861780

Comb Sort - The Sorting Algorithm Family Reunion

Comb Sort is the poster boy for the all-American child.

Unlike his elder brother Shell Sort, Comb Sort is a star athlete. He started as the quarterback for his high school football team when he was a sophomore. But more than that, he has everything he thought he could ever want, at least until recently. He’s the junior prom king, the one voted “best athlete” in the yearbook, everything. Life has been easy for him so far.

Yet, something is bothering him. Something that, until recently, he couldn’t describe.

He’s not the smartest guy, and he knows this. His grades are mostly Cs and Ds, and he understands that that doesn’t bode well for his future. His p

rents are high-achievers, his brother is a straight-A student, and he’s… what? A star athlete? That won’t last forever. What happens when he can no longer get by on physical prowess?

So, he reads. He studies. He’s putting more time into making good test scores and less time into sports. It’s not working yet (his last test was a C+), but he’s hopeful that it will, and soon. He can’t get through life on athletic skills alone. He’s sorting out his life, one piece at a time.

The Rundown

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.

Algorithm

  1. DETERMINE the appropriate gap size h by using a set shrink factor k.
  2. COMPARE each pair of elements that are h distance from each other.
  3. SWAP those elements if they are in the wrong order.
  4. COMPLETE a pass through the array with the selected h.
  5. SHRINK the gap size _h _using shrink factor k.
  6. IF h is not 1, GOTO step 2.
  7. CONTINUE until h is 1 AND a pass through the array results in no swaps.

Visualization


You can see from this animation why this algorithm got the name “Comb Sort”: the comparison distance look like two prongs in a comb. You can also see that this is a not-very-effective sort: it has to do many passes through all elements in order to fully sort this list. In fact, we would most likely consider Comb Sort to be a “naive” sort, on the same level as Selection Sort.

Also check out the “audibilization” of this sort from YouTube:

Finally, reader James Curran kindly created a visualization for this sort; it looks like this:


Implementation

class CombSort
{
static int GetNextGap(int gap)
{
//The “shrink factor”, empirically shown to be 1.3
gap = (gap * 10) / 13;
if (gap < 1)
{
return 1;
}
return gap;
}

static void Sort(int[] array)
{
    int length = array.Length;

    int gap = length;

    //We initialize this as true to enter the while loop.
    bool swapped = true;

    while (gap != 1 || swapped == true)
    {
        gap = GetNextGap(gap);

        //Set swapped as false.  Will go to true when two values are swapped.
        swapped = false;

        //Compare all elements with current gap 
        for (int i = 0; i < length - gap; i++)
        {
            if (array[i] > array[i + gap])
            {
                //Swap
                int temp = array[i];
                array[i] = array[i + gap];
                array[i + gap] = temp;

                swapped = true;
            }
        }
    }
}

public static void Main()
{
    int[] array = { 10, 28, 1, 55, 6, 21, 36, 3, 45, 15, 0 };

    Console.WriteLine("Comb Sort");

    CommonFunctions.PrintInitial(array);

    Sort(array);

    CommonFunctions.PrintFinal(array);
    Console.ReadLine();

}

}


### Time and Space Complexity

The worst case of _O(n2) _make intuitive sense, at least to me. Given that comb sort is based on bubble sort, we would expect poor time complexities for it. But the average case is interesting because it introduces a form of notation I hadn't seen before: big-omega.

Remember that the average time complexity for this algorithm is _Ω(n2/2p). _This is read aloud as "big-omega of n-squared over 2 to the power of p". But what does big-omega mean? In short, big-omega represents the best possible case of the complexity. On average, comb sort has been shown to perform no better than n2/2p, where _n_ is the length of the unsorted array and _p_ is the number of times we have to decrement the gap.

This is not a great time, and because it's the best possible case on average, we can conclude that Comb Sort is not an efficient sorting algorithm.

#exchange sorts #algorithms

Selection Sort - The Sorting Algorithm Family Reunion

Meet the patriarch of the sorting algorithm family! Selection Sort is the beloved father and grandfather to the rest of the gang.

He’s old, and likes things done a certain way, but at the heart of it all he’s a family man with a penchant for handing out Dum Dums and lecturing young’uns about days gone by. He and his darling wife Merge Sort will be doing the hosting duties for the family reunion at their large familial home, complete with an acre of land and a dog who quite enjoys chasing the kids around.

Ma! Come look at this! The dog got one o’ them squirrels again!

Boy, does he love that dog. Even better, the dog loves him. It won’t leave his side for a moment, except to chase the occasional squirrel, rabbit, or toddler. He’s getting up there in years, though, and Selection Sort dreads the day he has to explain to his young grandchildren where their beloved pet Rhythm has gone.

Selection Sort is one of the easiest sorting algorithms to understand, but also one of the worst-performing. That makes it a perfect candidate to kick off the reunion!

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. FOR EACH item in the collection…
  2. ASSUME the first item in the considered range is the smallest value
  3. ITERATE through the rest of the collection
  4. SWAP the smallest found value with the current value. This value is now sorted.
  5. ADJUST the considered range to exclude the now-sorted value.
  6. GO TO 2, CONTINUE until all elements have been sorted in this manner.

Visualization


The red box is the found lowest value. Yellow values are a sublist of the total list and have already been sorted; therefore the “considered range” mentioned in the algorithm above is all values that are not yellow. The blue value is the currently scanned value.

You may notice from watching this animation that this algorithm has to iterate through the collection many times. As discussed below, this is a major reason why this algorithm is not very efficient.

#selection sorts #algorithms