HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

Insertion sort revised with output to console

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
insertionwithoutputrevisedsortconsole

Problem

I took the question Performing insertion sort in C# and tidied it up a bit, then I thought about the use of the arrays and was wondering if it worked, and it does.

So now I have 3 static methods in a simple Program class to run from the console, just to check 15 numbers quickly. It's not very efficient.

What can be done to make it more efficient? I am assuming taking out the PrintIntegerArray from inside the PerformInsertionSort method for larger arrays would help with that.

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = new int[15] {9,4,13,42,56,21,1,19,42,42,40,109,3,8,99};

        Console.WriteLine("Final OutPut");
        PrintIntegerArray(PerformInsertionSort(numbers));
        Console.ReadLine();
    }

    static int[] PerformInsertionSort(int[] inputArray)
    {
        for (int i = 0; i  0; j--)
            {
                if (inputArray[j - 1] > inputArray[j])
                {
                    int temp = inputArray[j - 1];
                    inputArray[j - 1] = inputArray[j];
                    inputArray[j] = temp;
                }
                PrintIntegerArray(inputArray);
            }
        }
        return inputArray;   
    }

    public static void PrintIntegerArray(int[] array)
    {
        foreach (int i in array)
        {
            Console.Write(i.ToString() + ", ");
        }
        Console.WriteLine("End of Array");
        Console.ReadLine();
    }
}

Solution

One obvious optimization is to avoid many unnecessary swaps:

int j;
        value_to_insert = inputArray[i];
        for (j = i; j > 0; j--)
        {
            if (inputArray[j - 1] > value_to_insert)
            {
                inputArray[j] = inputArray[j - 1];
            }
        }
        inputArray[j] = value_to_insert;


Just one assignment instead of 3.

Besides (sorry for repeating my mantra on a naked loops), now it becomes obvious that an inner loop implements some sort of a shift algorithm. Better to factor it out.

It is also very well possible that separating find and shift phases may also speed it up:

value_to_insert = inputArray[i];
        int j = upper_bound(inputArray, inputArray + i, value_to_insert);
        shift(inputArray + j, input_array + i, 1);
        inputArray[j] = value_to_insert;


because both methods could be implemented as intrinsics.

Code Snippets

int j;
        value_to_insert = inputArray[i];
        for (j = i; j > 0; j--)
        {
            if (inputArray[j - 1] > value_to_insert)
            {
                inputArray[j] = inputArray[j - 1];
            }
        }
        inputArray[j] = value_to_insert;
value_to_insert = inputArray[i];
        int j = upper_bound(inputArray, inputArray + i, value_to_insert);
        shift(inputArray + j, input_array + i, 1);
        inputArray[j] = value_to_insert;

Context

StackExchange Code Review Q#60156, answer score: 3

Revisions (0)

No revisions yet.