Insertion Sort
Insertion Sort builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort, but it performs well for small datasets or nearly sorted datasets.
Steps:
- Start with the second element
- Compare it with the previous elements and insert it in the correct position
- Move to the next element and repeat until the array is sorted
Time Complexity
Best: O(n)
Average: O(n²)
Worst: O(n²)
Space Complexity
O(1)
Array length:
7
Speed: