Key Takeaways: Insertion Sort builds a sorted array one element at a time. It behaves exactly like how you might sort a hand of playing cards. It is highly efficient for small or nearly sorted datasets.
After learning Bubble Sort and Selection Sort, we move to our third foundational sorting algorithm: Insertion Sort.
This algorithm is very intuitive. It is the natural way humans tend to sort physical items!
Insertion Sort divides the array into a sorted part and an unsorted part, just like Selection Sort.
However, instead of looking for the smallest item, Insertion Sort simply picks up the very first unsorted item. It then scans backward through the sorted part and inserts the item into its exact correct position.
Imagine you are holding a hand of playing cards.
Insertion Sort: Pick the next element and shift existing elements right to make room for it.
Let's sort an array: [5, 3, 8, 4, 2]
Pass 1:
[5]. Unsorted: [3, 8, 4, 2].3. Compare it to 5.3 is smaller, so shift 5 to the right and insert 3.[3, 5, 8, 4, 2].Pass 2:
[3, 5]. Unsorted: [8, 4, 2].8. Compare it to 5.8 is larger, so it is already in the correct spot! Leave it alone.[3, 5, 8, 4, 2].Pass 3:
[3, 5, 8]. Unsorted: [4, 2].4. 4 is smaller than 8. Shift 8 right.4 is smaller than 5. Shift 5 right.4 is larger than 3. Insert 4 here![3, 4, 5, 8, 2].Pass 4:
2. Shift 8, 5, 4, 3 to the right. Insert 2 at the very beginning.[2, 3, 4, 5, 8].Let's write the Insertion Sort algorithm in Python.
Pay attention to the while loop that handles shifting the larger elements to the right!
def insertion_sort(arr): # Start from 1, because index 0 is already "sorted" for i in range(1, len(arr)): key = arr[i] # The element we want to insert j = i - 1 # Move elements that are greater than key # to one position ahead of their current position while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 # Insert the key in its correct position arr[j + 1] = key return arrnumbers = [5, 3, 8, 4, 2] print("Original:", numbers) print("Sorted:", insertion_sort(numbers))
O(n²). If the array is sorted in reverse, you have to shift every single element to the right for every new item you insert.O(n). This is where Insertion Sort shines! If the array is already sorted, the inner while loop never runs. It just checks each item once and moves on.O(1). Memory usage is minimal since it sorts in-place.Because of its O(n) best-case scenario, many modern programming languages actually use a hybrid of Insertion Sort and other algorithms (like Merge Sort) to handle small datasets under the hood!
Why does Insertion Sort have an amazing Best Case Time Complexity of O(n)?