Insertion Sort
✅ Insertion Sort
- Insert the element in the correct index
- start from index 2, sort the elements before index
- if the element is not bigger than
tmp
, insert the element andbreak
thefor loop
.
⭐️ Keyword
Insert an element in an already sorted array
✅ Code
https://soheeparklee.github.io/posts/CT-1-46/
✅ Time complexity
Worst case scenario(sorted as inverse) O(n^2).
Best case secnario, compare just once, thus, O(n).
✅ Space complexity
sort is done by swapping in the given array.
thus space complexity would be O(n)
👍🏻 Pros
- simple
- if the array is sorted, very efficient
- in-place sorting
- stable sort
- reletively faster than bubble, selection sort
👎🏻 Cons
- in worst scenario, time complexity inefficient
🆚 Selection Sort
- common: both sorts after repeating
k
times, the firstKth
element would be sorted. - Selection sort: to find
K+1th
element, need to search all remaining elements - Insertion sort: to find
K+1th
element, search only the elements that aresmaller
thanK+1th
If encounters element bigger thanK+1th
element, return and insert the element
💡 Reference
This post is licensed under CC BY 4.0 by the author.