In Focus

# Insertion Sorting Algorithm in C#

Sorting a numeric array using Insertion Sorting. Insertion sort is an elementary sorting algorithm that sorts one element at a time. The algorithm takes an element from the list and places it in the correct location in the list. This process is repeated until there are no more unsorted items in the list.

Insertion sort is an elementary sorting algorithm that sorts one element at a time. The algorithm takes an element from the list and places it in the correct location in the list. This process is repeated until there are no more unsorted items in the list. The computational complexity for insertion sort is O(n2).

Algorithm: Insertion Sort

It works the way you might sort a hand of playing cards:
1. We start with an empty left hand [sorted array] and the cards face down on the table [unsorted array].
2. Then remove one card [key] at a time from the table [unsorted array], and insert it into the correct position in the left hand [sorted array].
3. To find the correct position for the card, we compare it with each of the cards already in the hand, from right to left.
Pseudocode

We use a procedure INSERTION_SORT. It takes as parameters an array A[1.. n] and the length n of the array. The array A is sorted in place: the numbers are rearranged within the array, with mostly a constant number outside the array at any time.

INSERTION_SORT (A)
1. FOR j ← 2 TO length[A]
2. DO key ← A[j]
3. {Put A[j] into the sorted sequence A[1 . . j − 1]}
4. i ← j − 1
5. WHILE i > 0 and A[i] > key
6. DO A[i +1] ← A[i]
7. i ← i − 1
8. A[i + 1] ← key

1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Text;
5. namespace InsertionSort
6. {
7.     class Program
8.     {
9.         static int InsertionSorting()
10.         {
11.             Console.Write("\nProgram for sorting a numeric array using Insertion Sorting");
12.             Console.Write("\n\nEnter number of elements: ");
14.             int[] numarray = new int[max];
15.             for (int i = 0; i < max; i++)
16.             {
17.                 Console.Write("\nEnter [" + (i + 1).ToString() + "] element: ");
19.             }
20.             Console.Write("Input int array  : ");
21.             for (int k = 0; k < max; k++)
22.                 Console.Write(numarray[k] + " ");
23.             Console.Write("\n");
24.             for (int i = 1; i < max; i++)
25.             {
26.                 int j = i;
27.                 while (j > 0)
28.                 {
29.                     if (numarray[j - 1] > numarray[j])
30.                     {
31.                         int temp = numarray[j - 1];
32.                         numarray[j - 1] = numarray[j];
33.                         numarray[j] = temp;
34.                         j--;
35.                     }
36.                     else
37.                         break;
38.                 }
39.                 Console.Write("Iteration " + i.ToString() + ": ");
40.                 for (int k = 0; k < max; k++)
41.                     Console.Write(numarray[k] + " ");
42.                 Console.Write("\n");
43.                 //Console.Write("/*** " + (i + 1).ToString() + " numbers from the begining of the array are input and they are sorted ***/\n");
44.             }
45.             Console.Write("\n\nThe numbers in ascending orders are given below:\n\n");
46.             for (int i = 0; i < max; i++)
47.             {
48.                 Console.Write("Sorted [" + (i + 1).ToString() + "] element: ");
49.                 Console.Write(numarray[i]);
50.                 Console.Write("\n");
51.             }
52.             return 0;
53.         }
54.         static void Main(string[] args)
55.         {
56.             InsertionSorting();