Insertion Sort Algorithm Explained
Insertion Sort Algorithm Explained
Insertion sort determines the proper location for inserting a new element by comparing the new element, referred to as the key, with the elements to its left. It moves towards the left, shifting elements one position to the right if they are greater than the key, and stops when an element less than the key is found. The key is then inserted after this element. This ensures that the elements to the left of the key remain sorted .
Insertion sort begins by considering the second element of the array (index 1) as the key and compares it with the first element. It determines the position of this key relative to the first element and inserts it in the correct place. This process is repeated for each subsequent element until the array is sorted .
Insertion sort is preferred over bubble sort because it generally requires fewer comparisons and swaps, especially when dealing with partially sorted arrays. It is more efficient in terms of reducing the total number of steps when input arrays are partially sorted due to its adaptive nature .
Insertion sort deals with repeated values by maintaining their relative order. While shifting elements to insert a new element, if elements of the same value are encountered, insertion sort keeps them in their original order since no unnecessary swaps are performed when elements are equal, which preserves stability .
Insertion sort performs poorly on large datasets that are completely unsorted. This occurs because each new element must be compared with all previous elements in the worst case, resulting in a quadratic time complexity of O(n^2), inefficiently handling extensive comparisons and shifts .
Insertion sort is considered a stable sorting algorithm because it does not change the relative order of elements with equal keys. When elements are compared and found to be equal, insertion sort does not swap their positions, thus maintaining their original order .
When a key element is greater than all elements to its left in an insertion sort process, no shifting is required, and the key remains in its current position. This indicates that all elements are already sorted relative to the key value up to that point .
Insertion sort handles the insertion of the smallest element by comparing it all the way back to the beginning of the array. As it compares with each element to its left, each is shifted one position to the right until it reaches the start, where the smallest element is then placed in the first position of the sorted sub-array .
Insertion sort is adaptive, meaning that when elements are already sorted or partially sorted, it reduces the number of unnecessary comparisons and swaps. The loop detects when elements are in order, hence fewer operations are required, which makes it more efficient compared to fully unsorted data .
The space complexity of insertion sort is O(1) because it requires a constant amount of additional memory space beyond the original array. Only a single additional storage is needed for swapping elements during the sorting process, similar to bubble sort .