Rotate image (square matrix) by 90 deg
Tue, 06 May 2025
Insertion sort is the most common sorting technique used by card players to sort their cards. It sorts the array in-place and is the one of the best comparison sort algorithms.
The list is divided into two parts the way we did in Selection sort.
Pick the first element from the unsorted list and insert it at the right position in the sorted list (so that the sorted list is still sorted) as shown in the below example:
Example: Let the given array is as shown below, initially all the elements are in the unsorted part of the array.
Since, single element is always sorted, we will move first element in the sorted list, and start with the second element (i=1)
![]()
![]()


Code:
/** Non-recursive Insertion Sort */
void insertionSort(int *a, int n)
{
// For each element in the array
for(int i=1; i<n; i++)
{
int j, temp = a[i];
// Shift the element to right till we get an element smaller than
// current element or end of list.
for(j=i-1; j>=0 && a[j] > temp; j--)
a[j+1] = a[j];
// inserting element.
a[j+1] = temp;
}
}
Time Complexity:
Best Case: O(n) - When the array is already sorted in ascending order (In that case there will be only 1 check per element, hence O(n))
Worst case: O(n2) - When the array is sorted in reverse(descending) order. In that case each element will be inserted at the beginning of the list. Hence for kth element there will be (k-1) comparisons. So total work required = 1 + 2 + 3 + ... + n-1 = n(n-1)/2 = O(n2)
Average Case: Though theoretically the complexity is O(n2), practically it is less.
The constant factor of Insertion Sort is very tight (less), in comparison to other sorting algorithms. This makes it an obvious choice, if number of elements is small. If the array is almost sorted, then also Insertion sort is a very good choice. This is because for such cases (almost sorted arrays) insertion sort takes O(n) time and gives the best case performance.
Tue, 06 May 2025
Tue, 06 May 2025
Tue, 06 May 2025
Leave a comment