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:
- We start with an empty left hand [sorted array] and the cards face down on the table [unsorted array].
- 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].
- 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)
- FOR j ← 2 TO length[A]
- DO key ← A[j]
- {Put A[j] into the sorted sequence A[1 . . j − 1]}
- i ← j − 1
- WHILE i > 0 and A[i] > key
- DO A[i +1] ← A[i]
- i ← i − 1
- A[i + 1] ← key


Nilesh JadavPosted Sep 27, 2016, 1:21 PM
So far it is the best code ! Can you make this sorted output in desending form ? And can you check comparison between both ascending and descending with the help of count ? Can you help me with this code : http://www.c-sharpcorner.com/forums/worst-case-of-insertion-sort-in-c-sharp