Consider you have 10 cards out of a deck of cards in your hand. And they are sorted, or arranged in the ascending order of their numbers. If I give you another card, and ask you to insert the card in just the right position, so that the cards in your hand are still sorted. What will you do?
Well, you will have to go through each card from the starting or the back and find the right position for the new card, comparing its value with each card. Once you find the right position, you will insert the card there.
Similarly, if more new cards are provided to you, you can easily repeat the same process and insert the new cards and keep the cards sorted too.
This is exactly how insertion sort works.
It starts from the index 1(not 0), and each index starting from index 1 is like a new card, that you have to place at the right position in the sorted sub-array on the left.
Following are some of the important characteristics of Insertion Sort:
This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be inserted in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort.
Following are the steps involved in insertion sort:
Let's consider an array with values {5, 1, 6, 2, 4, 3}
Below, we have a pictorial representation of how insertion sort will sort the given array.
As you can see in the diagram above, after picking a key, we start iterating over the elements to the left of the key. We continue to move towards left if the elements are greater than the key element and stop when we find the element which is less than the key element. And, insert the key element after the element which is less than the key element.
#include <stdio.h>
void insertionSort(int[],int);
void printArray(int[],int);
// main function
int main()
{
int array[6] = {5, 1, 6, 2, 4, 3};
printf("Unsorted Array: ");
// print the unsorted array
printArray(array, 6);
// calling insertion sort function to sort the array
insertionSort(array, 6);
printf("\nSorted Array: ");
// print the sorted array
printArray(array, 6);
return 0;
}
void insertionSort(int arr[], int length)
{
int i, j, key;
for (i = 1; i < length; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
key = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = key;
j--;
}
}
}
// function to print the given array
void printArray(int array[], int size)
{
int j;
for (j = 0; j < size; j++)
{
printf("%d ",array[j]);
}
}
Unsorted Array: 5 1 6 2 4 3
Sorted Array: 1 2 3 4 5 6
Now we have a bigger picture of how this sorting technique works, so we can derive simple steps by which we can achieve insertion sort.
procedure insertionSort( A : array of items )
int holePosition
int valueToInsert
for i = 1 to length(A) inclusive do:
/* select value to be inserted */
valueToInsert = A[i]
holePosition = i
/*locate hole position for the element to be inserted */
while holePosition > 0 and A[holePosition-1] > valueToInsert do:
A[holePosition] = A[holePosition-1]
holePosition = holePosition -1
end while
/* insert the number at hole position */
A[holePosition] = valueToInsert
end for
end procedure
Data Structures
Data Structures Overview Arrays Linked List Stack Queue Priority QueueExpression parsing
Infix Notation Prefix Notation Postfix notation Notation ConversionHashing
Hash Table and Function Collision ResolutionSearching Techniques
Linear Search Binary Search Interpolation Search Fibonacci SearchSorting
Bubble Sort Bubble Sort in Depth Selection Sort Selection Sort in Depth Insertion Sort Insertion Sort in Depth Shell Sort Merge Sort Quick Sort Quick Sort in Depth Counting Sort Radix Sort Bucket SortTree
Trees and Graphs Binary Tree Types of Binary Trees Binary Search Tree N-ary Tree AVL Tree Red Black Tree Splay tree B tree B+ treeTree Search Traversal Techniques
Breadth First Search (BFS) Depth First Search (DFS) Inorder, Preorder and Postorder TraversalHeaps
Heaps Introduction Binary Heap Heap OperationsTrending
C Programming
Remember to follow community guidelines and don't go off topic.