Sorting Algorithms
Insertion Sort
Vanilla Insertion Sort
Vanilla Insertion Sort
1 2 3 4 |
|

This above version has \(\theta(n)\) steps and each step has \(\theta(n)\) comparisons. SO this version of the algorithm is \(\theta(n^2)\) runtime complexity.
Binary Insertion Sort
Binary Insertion Sort
This improved version is slightly improved by using Binary Search while searching for the position to place the key A[i]
in the sorted part of the array (i.e. A[0:i-1]
)
1 2 3 4 |
|
This above version has \(\theta(n)\) steps and each step has \(\theta(\log(n))\) comparisons due to Binary Search. SO this version of the algorithm is \(\theta(n\times \log(n))\) runtime complexity (in sorting but not in swappings). If we consider swapping operations too then even Binary Search will take \(\theta(n)\) time to swap positions as it might have to move a lot of positions.