Insertion Sort
Why did the insertion sort algorithm get invited to all the parties? Because it knew how to slide into the right place without causing a scene!
Last updated
Why did the insertion sort algorithm get invited to all the parties? Because it knew how to slide into the right place without causing a scene!
Last updated
Insertion Sort is a simple comparison-based sorting algorithm that builds the sorted portion of the list one element at a time. It works by repeatedly taking the next unsorted element and inserting it into its correct position within the sorted portion of the list. Insertion Sort is named because it inserts elements into their proper place.
Here's a step-by-step explanation of how Insertion Sort works:
Start with the first element as the sorted region (since a single element is always sorted).
For each unsorted element, compare it to elements in the sorted region from right to left until you find the correct position.
Insert the element into its correct position within the sorted region, shifting other elements to the right if necessary.
Expand the sorted region to include the newly added element.
Repeat steps 2-4 for each unsorted element until the entire list becomes sorted.
Insertion Sort is particularly efficient when dealing with small datasets or partially sorted data, as the number of comparisons and swaps is minimal when elements are already in the correct order.
The time complexity of Insertion Sort can be expressed mathematically as follows:
Let n be the number of elements in the array to be sorted.
In the best-case scenario, when the input array is already sorted, Insertion Sort makes only n - 1 comparisons (each element compared once) and no swaps.
In the worst-case scenario, when the input array is sorted in reverse order, Insertion Sort makes the maximum number of comparisons and swaps. In this case, for each element, it compares it to all the previous elements in the sorted region.
The total number of comparisons and swaps in the worst-case scenario can be calculated as the sum of the first (n-1) natural numbers, which is given by the formula:
Therefore, the worst-case time complexity of Insertion Sort can be mathematically expressed as:
Asymptotically, the worst-case time complexity of Insertion Sort is O(n^2), which indicates that its performance degrades quadratically with the size of the input array. In the best-case scenario, when the input array is nearly sorted, the time complexity can be as low as O(n).
So the time complexity is:
Worst-case: O(n^2)
Average-case: O(n^2)
Best-case: O(n) when the list is nearly sorted
And the space complexity: O(1) (in-place sorting, no additional memory required)
Insertion Sort is efficient for small datasets or when dealing with partially sorted data. It performs well when the list is nearly sorted (best-case scenario). However, its worst-case time complexity of O(n^2) makes it inefficient for sorting large datasets compared to more efficient sorting algorithms like Quick Sort or Merge Sort.
So, in the worst-case scenario, the number of comparisons and swaps made by Insertion Sort is approximately