Insertion Sort Algorithm in DSA Python - Time & Space Complexity
We want to understand how the time taken by the Insertion Sort algorithm changes as the list size grows.
Specifically, how many steps does it take to sort a list of numbers?
Analyze the time complexity of the following Insertion Sort code.
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
This code sorts the list by inserting each element into its correct place among the sorted elements to its left.
- Primary operation: The inner while loop that shifts elements to insert the key.
- How many times: The outer for loop runs n-1 times, and the inner while loop can run up to i times for each i.
As the list size grows, the number of comparisons and shifts grows roughly with the square of the size in the worst case.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 45 comparisons and shifts |
| 100 | About 4,950 comparisons and shifts |
| 1000 | About 499,500 comparisons and shifts |
Pattern observation: The operations grow roughly by the square of the input size, so doubling the list size roughly quadruples the work.
Time Complexity: O(n²)
This means the time taken grows roughly with the square of the number of items to sort.
[X] Wrong: "Insertion Sort always takes the same time no matter the input order."
[OK] Correct: If the list is already mostly sorted, Insertion Sort runs much faster, closer to linear time, because fewer shifts are needed.
Understanding Insertion Sort's time complexity helps you compare simple sorting methods and recognize when they are efficient or not.
"What if we changed the inner while loop to stop early when the list is already sorted? How would the time complexity change?"
Practice
Insertion Sort algorithm?Solution
Step 1: Understand insertion sort process
Insertion sort builds a sorted list by taking one element at a time and placing it in the correct position among the already sorted elements.Step 2: Compare with other sorting methods
Options A, B, and D describe other sorting algorithms: bubble sort, merge sort, and selection sort respectively.Final Answer:
Build the sorted list one element at a time by inserting elements in their correct position -> Option AQuick Check:
Insertion sort = Insert elements in order [OK]
- Confusing insertion sort with bubble sort
- Thinking insertion sort divides the list
- Mixing insertion sort with selection sort
arr of length n in Python?Solution
Step 1: Identify insertion sort loop start
The outer loop starts from index 1 because the first element (index 0) is considered sorted.Step 2: Check options
for i in range(1, n): correctly usesrange(1, n). Options B and C start from 0 which is incorrect. for i in range(n-1, 0, -1): loops backwards which is not standard for insertion sort.Final Answer:
for i in range(1, n): -> Option CQuick Check:
Start loop at 1 for insertion sort [OK]
- Starting loop at 0 instead of 1
- Using reverse loop incorrectly
- Confusing loop ranges
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
arr = [4, 3, 2, 1]
insertion_sort(arr)
print(arr)Solution
Step 1: Trace the insertion sort on input
The list [4, 3, 2, 1] is sorted step-by-step by moving smaller elements leftwards until fully sorted.Step 2: Final sorted list
After sorting, the list becomes [1, 2, 3, 4].Final Answer:
[1, 2, 3, 4] -> Option BQuick Check:
Sorted ascending = [1, 2, 3, 4] [OK]
- Assuming original list remains unchanged
- Confusing output with partial sorting
- Misreading the while loop condition
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i
while j > 0 and arr[j-1] > key:
arr[j] = arr[j-1]
j -= 1
arr[j] = key
arr = [3, 1, 2]
insertion_sort(arr)
print(arr)Solution
Step 1: Check initialization of j
In insertion sort, j should start at i - 1 to compare the key with previous elements using the standard condition arr[j] > key.Step 2: Verify other parts
The while loop condition and assignment are correct. The for loop correctly starts at 1.Final Answer:
The variable j should start at i - 1, not i -> Option DQuick Check:
j = i - 1 to compare previous elements [OK]
- Using j = i instead of standard j = i-1
- Changing while loop condition incorrectly
- Misplacing key assignment inside loop
arr = [5, 3, 8, 6, 2], how many total shifts (element moves) will insertion sort perform to sort the list in ascending order?Solution
Step 1: Trace shifts during sorting
Insertion sort shifts elements to insert the key in the correct place:
- i=1 (key=3): shift 5 right (1 shift)
- i=2 (key=8): no shift (0 shifts)
- i=3 (key=6): shift 8 right (1 shift)
- i=4 (key=2): shift 8, 6, 5, 3 right (4 shifts)Step 2: Calculate total shifts
Total shifts = 1 + 0 + 1 + 4 = 6 shifts. But note that each shift moves one element one position right. Counting carefully:
At i=1: 1 shift
At i=3: 1 shift
At i=4: 4 shifts
Total = 6 shiftsStep 3: Recount carefully
Actually, at i=4, elements 3,5,6,8 are shifted right one by one (4 shifts). Total shifts = 1 + 0 + 1 + 4 = 6.Final Answer:
6 -> Option AQuick Check:
Count shifts per insertion = 6 [OK]
- Counting comparisons instead of shifts
- Missing multiple shifts in one insertion
- Confusing shifts with swaps
Ready to prove it?
Pass the test and this pattern moves unknown
