Insertion Sort Algorithm in DSA C++ - Time & Space Complexity
We want to understand how the time taken by Insertion Sort changes as the list size grows.
How does the number of steps increase when sorting more items?
Analyze the time complexity of the following code snippet.
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
This code sorts an array by inserting each element into its correct position among the previously sorted elements.
Identify the loops, recursion, array traversals that repeat.
- 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 from 1 to n-1.
As the list size grows, the number of comparisons and shifts increases 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 input makes the work about four times bigger.
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 explain sorting basics clearly and compare simple algorithms to more advanced ones.
"What if we changed the array to be nearly sorted? How would the time complexity change?"
Practice
Insertion Sort algorithm?Solution
Step 1: Understand the insertion process
Insertion sort takes one element at a time and inserts it into the correct position in the already sorted part of the array.Step 2: Compare with other sorting methods
Unlike divide and conquer (merge sort) or swapping adjacent elements (bubble sort), insertion sort builds the sorted array gradually by insertion.Final Answer:
Build the sorted array one element at a time by inserting elements in their correct position -> Option CQuick Check:
Insertion sort = Insert elements correctly one by one [OK]
- Confusing insertion sort with bubble sort
- Thinking it divides array like merge sort
- Assuming it swaps only smallest elements
Solution
Step 1: Identify the inner loop role
The inner loop moves backward from the current element's previous index, shifting elements greater than the key to the right.Step 2: Match the correct loop condition
The loop continues while j >= 0 and arr[j] > key, decrementing j each time.Final Answer:
for (int j = i - 1; j >= 0 && arr[j] > key; j--) -> Option BQuick Check:
Inner loop moves backward while arr[j] > key [OK]
- Using j++ instead of j-- in inner loop
- Checking arr[j] < key instead of > key
- Starting inner loop from wrong index
arr = {4, 3, 2, 1}?Solution
Step 1: Trace first iteration
Insert 3 into sorted part {4}: shift 4 right, array becomes {3,4,2,1}.Step 2: Trace second iteration
Insert 2 into {3,4}: shift 4 and 3 right, array becomes {2,3,4,1}.Step 3: Trace third iteration
Insert 1 into {2,3,4}: shift all right, array becomes {1,2,3,4}.Final Answer:
[1, 2, 3, 4] -> Option DQuick Check:
Sorted ascending = [1, 2, 3, 4] [OK]
- Assuming array stays same if reversed
- Stopping after first iteration
- Not shifting all larger elements
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}Solution
Step 1: Understand the while loop condition
Insertion sort shifts elements greater than key to the right, so condition must be arr[j] > key.Step 2: Identify the incorrect comparison
Current code uses arr[j] < key, which shifts smaller elements incorrectly, breaking sorting.Final Answer:
The comparison in while should be arr[j] > key, not arr[j] < key -> Option AQuick Check:
Shift elements greater than key, so condition is arr[j] > key [OK]
- Using wrong comparison sign in while condition
- Starting loop from wrong index
- Misplacing key assignment inside loop
arr = {5, 2, 9, 1, 5, 6}, how many total shifts (element moves) will insertion sort perform to sort it?Solution
Step 1: Insert 2 into {5}
Shift 5 right (1 shift). Array: {2, 5, 9, 1, 5, 6}Step 2: Insert 9 into {2, 5}
No shift needed since 9 > 5 (0 shifts).Step 3: Insert 1 into {2, 5, 9}
Shift 9, 5, 2 right (3 shifts). Array: {1, 2, 5, 9, 5, 6}Step 4: Insert 5 into {1, 2, 5, 9}
Shift 9 right (1 shift, stops at 5 == 5). Array: {1, 2, 5, 5, 9, 6}Step 5: Insert 6 into {1, 2, 5, 5, 9}
Shift 9 right (1 shift). Array: {1, 2, 5, 5, 6, 9}Final Answer:
6 -> Option AQuick Check:
Total shifts = 1 + 0 + 3 + 1 + 1 = 6 [OK]
- Counting only swaps, not shifts
- Missing shifts for repeated elements
- Confusing key placement with shifts
Ready to prove it?
Pass the test and this pattern moves unknown
