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 place 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 grows, the number of comparisons and shifts grows roughly with the square of the size.
| 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: Doubling the input size roughly quadruples the work, showing a square growth pattern.
Time Complexity: O(n²)
This means the time to sort grows roughly with the square of the number of items.
[X] Wrong: "Insertion Sort always takes the same time no matter the input order."
[OK] Correct: If the list is already sorted, Insertion Sort runs much faster, doing only one comparison per element, so the time is closer to linear.
Understanding Insertion Sort's time complexity helps you explain sorting basics and compare simple algorithms confidently.
"What if we changed the array to be mostly sorted? How would the time complexity change?"
Practice
Insertion Sort algorithm?Solution
Step 1: Understand insertion sort process
Insertion sort builds the 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
Other options describe different algorithms: divide and conquer is merge sort, swapping adjacent elements is bubble sort, and heap data structure is heap sort.Final Answer:
Build the sorted array one element at a time by inserting elements in the correct position -> Option AQuick Check:
Insertion sort = Insert elements step-by-step [OK]
- Confusing insertion sort with bubble sort
- Thinking insertion sort divides array recursively
- Mixing up heap sort with insertion sort
Solution
Step 1: Identify inner loop role in insertion sort
The inner loop moves backward from the current element's previous index, shifting elements greater than the key to the right.Step 2: Match syntax with this logic
for (int j = i - 1; j >= 0 && arr[j] > key; j--) correctly uses a loop starting from i-1 down to 0, checking if arr[j] > key and decrementing j.Final Answer:
for (int j = i - 1; j >= 0 && arr[j] > key; j--) -> Option CQuick Check:
Inner loop moves backward while arr[j] > key [OK]
- Using forward loop instead of backward
- Not checking arr[j] > key condition
- Incorrect loop boundaries
int arr[] = {4, 3, 2, 1};?Solution
Step 1: Trace insertion sort on given array
Start with 4, then insert 3 before 4, array becomes [3,4,2,1]. Insert 2 before 3, array becomes [2,3,4,1]. Insert 1 before 2, array becomes [1,2,3,4].Step 2: Confirm final sorted array
After all insertions, the array is sorted ascending: [1, 2, 3, 4].Final Answer:
[1, 2, 3, 4] -> Option DQuick Check:
Sorted ascending = [1, 2, 3, 4] [OK]
- Assuming array stays the same
- Mixing up partial sorted arrays
- Not moving elements correctly
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 insertion sort comparison logic
Insertion sort shifts elements greater than key to the right, so condition must be arr[j] > key.Step 2: Check given code's while condition
The code uses arr[j] < key, which incorrectly shifts smaller elements, breaking sorting.Final Answer:
The comparison in while loop should be arr[j] > key, not arr[j] < key -> Option BQuick Check:
Correct comparison is arr[j] > key [OK]
- Using < instead of > in while condition
- Starting loop from wrong index
- Misplacing key assignment
int arr[] = {5, 2, 9, 1, 5, 6};, how many total shifts (element moves) does insertion sort perform to sort it?Solution
Step 1: Trace insertion sort shifts step-by-step
Start with array: [5,2,9,1,5,6]
i=1 (key=2): shift 5 right (1 shift) -> [2,5,9,1,5,6]
i=2 (key=9): no shift (0 shifts)
i=3 (key=1): shift 9, then 5, then 2 right (3 shifts) -> [1,2,5,9,5,6]
i=4 (key=5): shift 9 right (1 shift) -> [1,2,5,5,9,6]
i=5 (key=6): shift 9 right (1 shift) -> [1,2,5,5,6,9]
Total shifts = 1 + 0 + 3 + 1 + 1 = 6 shiftsStep 2: Confirm total shifts
Counting carefully, total shifts are 6.Final Answer:
6 -> Option AQuick Check:
Count shifts carefully = 6 [OK]
- Counting only key placements, not shifts
- Missing multiple shifts in one iteration
- Confusing swaps with shifts
Ready to prove it?
Pass the test and this pattern moves unknown
