Insertion Sort Explained with Example
Insertion Sort Explained with Example
In one pass of the insertion sort algorithm, the following steps occur: 1) Start from the element right after the sorted section. 2) Compare the current element with the last element of the sorted section. 3) If the current element is smaller, shift the compared element one position to the right. 4) Repeat the comparison process until the appropriate position is found or the start of the sorted section is reached. 5) Insert the current element into its correct position within the sorted section .
Insertion sort maintains separate 'sorted' and 'unsorted' sections by initially considering the first element as sorted. It increments the sorted section one element at a time as elements from the unsorted section are iteratively inserted at the correct position within the sorted section. This strategy is effective because it incrementally builds the solution, allowing the sorted section to grow in an orderly fashion while ensuring each element is correctly positioned before proceeding to the next, thus providing a stable sorting process .
Insertion sort determines where to place an element by comparing it with elements in the sorted section of the array, starting from the end of the sorted section. If the current element is smaller than the element it is being compared with, the compared element is shifted one position to the right. This process continues until an appropriate position is found where the current element is larger than the element it is being compared with or the beginning of the sorted section is reached .
Insertion sort treats the initial element of the array as already sorted. This provides the starting point for building the sorted section of the array. By considering this element sorted, insertion sort has a basis to begin comparing subsequent elements and determining where they should be placed relative to this initial element, facilitating the incremental construction of the full sorted array .
Comparisons in insertion sort are critical for maintaining order in the sorted section of the array. Each comparison checks if the current element from the unsorted section is smaller than elements in the sorted section. These comparisons determine the position where the current element should be inserted. Fewer comparisons indicate the list is closer to being sorted, leading to fewer element shifts, faster sorting, and less resource usage .
Insertion sort might be preferred for small datasets because of its simple implementation and efficient handling of already nearly-sorted data, resulting in minimal movement of elements. The overhead is low compared to more complex algorithms, making it faster for small tasks. It’s also an adaptive sort, meaning its performance improves significantly with partial datasets that are already partially sorted .
Element shifting in insertion sort occurs when an element from the unsorted section is smaller than elements in the sorted section. Each element in the sorted section that is larger than the current element is shifted one position to the right, creating space for the current element to be inserted at the correct position. This method contributes to the maintenance of order within the sorted section, ensuring that all elements are placed in their final sorted order by the end of the sorting process .
Insertion sort achieves its ideal performance when the input array is already sorted. In this best-case scenario, insertion sort runs with a time complexity of O(n) because each element is only compared once and no shifting of elements is required, resulting in a highly efficient linear time performance .
In the best-case scenario, where the array is already sorted, insertion sort has a time complexity of O(n) because each element only needs to be compared once. However, in the worst-case scenario, where the array is sorted in reverse order, the time complexity is O(n^2) because each element must be compared with every other element before it can be inserted into its correct position, leading to a quadratic number of total comparisons and shifts .
The first iteration of insertion sort is distinguished by the initial setup where the first element of the array is considered sorted on its own. Subsequent elements are then iteratively picked from the unsorted section beginning from the second element, which are then inserted into the correct position within the growing sorted section of the array. Each subsequent iteration involves comparison and possible shifting of elements already sorted .