Sorting
Insertion sort:
The same approach is applied in insertion sort. The idea behind the insertion sort is that first take one element, iterate it through the sorted array. Although it is simple to use, it is not appropriate for large data sets as the time complexity of insertion sort in the average case and worst case is O(n2), where n is the number of items. Insertion sort is less efficient than the other sorting algorithms like heap sort, quick sort, merge sort, etc.
The simple steps of achieving the insertion sort are listed as follows
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step2 - Pick the next element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move to the next element. Else, shift greater elements in the array towards the right.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
Working of Insertion
sort Algorithm:
To understand the working of the insertion sort algorithm,
let's take an unsorted array. It will be easier to understand the insertion
sort via an example.
Let the elements of array are
Initially, the first two elements are compared in insertion sort.
Here, 25 is smaller than 31. So, 31 is not at correct
position. Now, swap 31 with 25. Along with swapping, insertion sort will also
check it with all elements in the sorted array.
For now, the sorted array has only one element, i.e. 12. So, 25 is greater than 12. Hence, the sorted array remains sorted after swapping
Now, two elements in the sorted array are 12 and 25. Move forward to the next elements that are 31 and 8.
Both 31 and 8 are not sorted. So, swap them.
Both 31 and 8 are not sorted. So, swap them.
After swapping, elements 25 and 8 are unsorted.
So, swap them.
Now, the sorted array has three items that are 8, 12 and 25.
Move to the next items that are 31 and 32.
Hence, they are already sorted. Now, the sorted array includes 8, 12, 25 and 31.
Move to the next elements that are 32 and 17.
17 is smaller than 32. So, swap them.
Swapping makes 31 and 17 unsorted. So, swap them too.
Now, swapping makes 25 and 17 unsorted. So, perform swapping again.
Now, the array is completely sorted.
Insertion sort complexity:
Time Complexity:
Best Case Complexity - It occurs when there is no sorting
required, i.e. the array is already sorted. The best-case time complexity of
insertion sort is O(n).
Average Case Complexity - It occurs when the array elements
are in jumbled order that is not properly ascending and not properly
descending. The average case time complexity of insertion sort is O(n2).
Worst Case Complexity - It occurs when the array elements are required to be sorted in reverse order. That means suppose you have to sort the array elements in ascending order, but its elements are in descending order. The worst-case time complexity of insertion sort is O(n2).
#include <stdio.h>
void insert(int a[], int n) /* function to sort an aay with insertion sort */
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one position ahead from their current position*/
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
void printArr(int a[], int n) /* function to print the array */
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
insert(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
Advantages of Insertion Sort
· It is easy to implement and efficient to use on small sets of data.
· It can be efficiently implemented on data sets that are already substantially sorted.
· It performs better than algorithms like selection sort and bubble sort. Insertion sort algorithm is simpler than shell sort, with only a small trade-off in efficiency. It is over twice as fast as the bubble sort and almost 40 per cent faster than the selection sort.
· It requires less memory space (only O(1) of additional memory space).
· It is said to be online, as it can sort a list as and when it receives new elements.
****************************************************