What is the basic idea behind the Insertion Sort Algorithm?
Insertion Sort builds the sorted list one element at a time by taking each element and inserting it into its correct position among the previously sorted elements.
Click to reveal answer
beginner
How does Insertion Sort handle the elements during sorting?
It picks the next element and compares it backward with the sorted part, shifting larger elements one position ahead to make space for the new element.
Click to reveal answer
intermediate
What is the time complexity of Insertion Sort in the worst case?
The worst-case time complexity is O(n²), which happens when the input list is sorted in reverse order.
Click to reveal answer
intermediate
Why is Insertion Sort efficient for nearly sorted data?
Because it only shifts elements when necessary, so if the data is almost sorted, fewer shifts are needed, making it close to O(n) time.
Click to reveal answer
beginner
Show the first three steps of Insertion Sort on the array [5, 3, 8, 6].
Step 1: [5, 3, 8, 6] (start with first element sorted)
Step 2: [3, 5, 8, 6] (insert 3 before 5)
Step 3: [3, 5, 8, 6] (8 is already in correct place)
Click to reveal answer
What does Insertion Sort do with each new element it processes?
ASwaps it with the last element
BInserts it into the correct position in the sorted part
CIgnores it if it is larger than the previous
DRemoves duplicates
✗ Incorrect
Insertion Sort inserts each new element into its correct position among the already sorted elements.
What is the best-case time complexity of Insertion Sort?
AO(n)
BO(n log n)
CO(n²)
DO(log n)
✗ Incorrect
When the array is already sorted, Insertion Sort only compares each element once, resulting in O(n) time.
Which of these is true about Insertion Sort?
AIt requires extra memory proportional to input size
BIt sorts by dividing the array into halves
CIt is faster than Quick Sort on large random data
DIt is a stable sorting algorithm
✗ Incorrect
Insertion Sort is stable because it does not change the relative order of equal elements.
In Insertion Sort, what happens when the current element is smaller than elements in the sorted part?
AThe current element is swapped with the first element
BThe current element is discarded
CThose elements are shifted right to make space
DThe array is reversed
✗ Incorrect
Elements larger than the current element are shifted one position to the right to insert the current element correctly.
Which scenario causes the worst-case performance for Insertion Sort?
AArray is sorted in reverse order
BArray is already sorted
CArray has all equal elements
DArray is empty
✗ Incorrect
When the array is sorted in reverse order, Insertion Sort must shift every element for each insertion, causing O(n²) time.
Explain how Insertion Sort works step-by-step on a small array.
Think about sorting playing cards in your hand.
You got /5 concepts.
Describe the time complexity of Insertion Sort and when it performs best and worst.
Consider how many shifts happen depending on the order of the input.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Insertion Sort algorithm?
easy
A. Build the sorted array one element at a time by inserting elements in the correct position
B. Divide the array into halves and sort each half recursively
C. Swap adjacent elements if they are in the wrong order repeatedly
D. Use a heap data structure to sort elements
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 A
Quick Check:
Insertion sort = Insert elements step-by-step [OK]
Hint: Insertion sort inserts elements one by one in order [OK]
Common Mistakes:
Confusing insertion sort with bubble sort
Thinking insertion sort divides array recursively
Mixing up heap sort with insertion sort
2. Which of the following is the correct syntax for the inner loop in insertion sort in C?
easy
A. for (int j = n - 1; j > i; j--)
B. for (int j = 0; j < i; j++)
C. for (int j = i - 1; j >= 0 && arr[j] > key; j--)
D. for (int j = i + 1; j < n; j++)
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 C
Quick Check:
Inner loop moves backward while arr[j] > key [OK]
Hint: Inner loop goes backward checking elements > key [OK]
Common Mistakes:
Using forward loop instead of backward
Not checking arr[j] > key condition
Incorrect loop boundaries
3. What will be the output array after running insertion sort on int arr[] = {4, 3, 2, 1};?
medium
A. [4, 3, 2, 1]
B. [1, 3, 2, 4]
C. [2, 3, 4, 1]
D. [1, 2, 3, 4]
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].