What is the basic idea behind the Insertion Sort algorithm?
Insertion Sort builds the final sorted list one item at a time by taking each element and inserting it into its correct position among the previously sorted elements.
Click to reveal answer
beginner
What is the time complexity of Insertion Sort in the worst case?
The worst-case time complexity of Insertion Sort is O(n²), where n is the number of elements to sort. This happens when the list is sorted in reverse order.
Click to reveal answer
intermediate
How does Insertion Sort behave with nearly sorted data?
Insertion Sort performs very efficiently on nearly sorted data, with a time complexity close to O(n), because fewer shifts are needed to insert elements in their correct positions.
Click to reveal answer
beginner
Show the step-by-step insertion of the element 5 into the sorted list [1, 3, 7, 9] using Insertion Sort.
Step 1: Compare 5 with 9, shift 9 right. Step 2: Compare 5 with 7, shift 7 right. Step 3: Compare 5 with 3, stop shifting. Step 4: Insert 5 after 3. Resulting list: [1, 3, 5, 7, 9]
Click to reveal answer
beginner
What is the space complexity of Insertion Sort?
Insertion Sort has a space complexity of O(1) because it sorts the list in place without needing extra space proportional to the input size.
Click to reveal answer
What does Insertion Sort do at each step?
ASelects the smallest element and swaps it with the first
BInserts the current element into the correct position in the sorted part
CDivides the list into two halves
DSwaps the current element with the last element
✗ Incorrect
Insertion Sort takes the current element and inserts it into the correct position among the already sorted elements.
What is the best-case time complexity of Insertion Sort?
AO(n)
BO(n²)
CO(n log n)
DO(log n)
✗ Incorrect
When the list is already sorted, Insertion Sort only compares each element once, resulting in O(n) time.
Which of these is true about Insertion Sort?
AIt divides the list into sublists
BIt requires extra memory proportional to the input size
CIt always performs better than Quick Sort
DIt is stable and sorts in place
✗ Incorrect
Insertion Sort is stable (preserves order of equal elements) and sorts the list in place without extra memory.
In Insertion Sort, what happens when the current element is smaller than elements in the sorted part?
AThose elements are shifted right to make space
BThe current element is discarded
CThe list is reversed
DThe current element is swapped with the first element
✗ Incorrect
Elements larger than the current element are shifted right to insert the current element in the correct position.
Which scenario causes the worst-case performance for Insertion Sort?
AList contains all equal elements
BList is already sorted
CList is sorted in reverse order
DList is empty
✗ Incorrect
When the list is sorted in reverse order, every element must be compared and shifted, causing O(n²) time.
Explain how Insertion Sort works step-by-step on a small list.
Think about sorting playing cards in your hand.
You got /4 concepts.
Describe the time and space complexity of Insertion Sort and when it performs best.
Consider how many comparisons and shifts happen depending on input order.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Insertion Sort algorithm?
easy
A. Divide the array into halves and sort each half recursively
B. Swap adjacent elements if they are in the wrong order repeatedly
C. Build the sorted array one element at a time by inserting elements in their correct position
D. Select the smallest element and swap it with the first unsorted element
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 C
Quick Check:
Insertion sort = Insert elements correctly one by one [OK]
Hint: Insertion sort inserts elements step-by-step in order [OK]
Common Mistakes:
Confusing insertion sort with bubble sort
Thinking it divides array like merge sort
Assuming it swaps only smallest elements
2. Which of the following is the correct syntax for the inner loop condition in insertion sort in C++?
easy
A. for (int j = i + 1; j < n && arr[j] < key; j++)
B. for (int j = i - 1; j >= 0 && arr[j] > key; j--)
C. for (int j = 0; j < i && arr[j] < key; j++)
D. for (int j = i - 1; j >= 0 && arr[j] < key; j--)
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 B
Quick Check:
Inner loop moves backward while arr[j] > key [OK]
Hint: Inner loop moves backward while element is greater than key [OK]
Common Mistakes:
Using j++ instead of j-- in inner loop
Checking arr[j] < key instead of > key
Starting inner loop from wrong index
3. What will be the output array after running insertion sort on arr = {4, 3, 2, 1}?
medium
A. [2, 3, 4, 1]
B. [4, 3, 2, 1]
C. [1, 3, 2, 4]
D. [1, 2, 3, 4]
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 D
Quick Check:
Sorted ascending = [1, 2, 3, 4] [OK]
Hint: Insertion sort sorts ascending by shifting larger elements right [OK]
Common Mistakes:
Assuming array stays same if reversed
Stopping after first iteration
Not shifting all larger elements
4. Identify the error in this insertion sort snippet:
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;
}
medium
A. The comparison in while should be arr[j] > key, not arr[j] < key
B. The loop should start from i = 0 instead of i = 1
C. The assignment arr[j + 1] = key should be outside the loop
D. The decrement j-- should be j++ inside the while loop
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 A
Quick Check:
Shift elements greater than key, so condition is arr[j] > key [OK]
Hint: While loop compares arr[j] > key to shift larger elements [OK]
Common Mistakes:
Using wrong comparison sign in while condition
Starting loop from wrong index
Misplacing key assignment inside loop
5. Given an array arr = {5, 2, 9, 1, 5, 6}, how many total shifts (element moves) will insertion sort perform to sort it?