What is the main 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 already sorted elements.
Click to reveal answer
beginner
How does Insertion Sort handle the elements during sorting?
It picks elements one by one from the unsorted part and inserts them into the correct position in the sorted part by shifting larger elements to the right.
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²), where n is the number of elements, because each insertion may require shifting all previously sorted elements.
Click to reveal answer
intermediate
Why is Insertion Sort efficient for nearly sorted data?
Because it only shifts elements when necessary, if the data is almost sorted, fewer shifts are needed, making it close to O(n) time.
Click to reveal answer
beginner
Show the state of the array after inserting the element 5 into the sorted subarray [1, 3, 7, 9].
After inserting 5, the array becomes [1, 3, 5, 7, 9]. The element 5 is placed between 3 and 7 by shifting 7 and 9 to the right.
Click to reveal answer
What does Insertion Sort do with each new element from the unsorted part?
AInserts it into the correct position in the sorted part
BSwaps it with the last element
CIgnores it if it is smaller
DDeletes it
✗ Incorrect
Insertion Sort takes each element and inserts it into the correct position in the sorted part by shifting elements as needed.
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 is the fastest sorting algorithm for large data
BIt always uses extra memory
CIt sorts by dividing the array into halves
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
BThose elements are shifted right to make space
CThe array is reversed
DThe current element is discarded
✗ Incorrect
Elements larger than the current element are shifted right to insert the current element in the correct position.
Which scenario makes Insertion Sort perform poorly?
AArray is sorted in reverse order
BArray is already sorted
CArray is nearly sorted
DArray has all equal elements
✗ Incorrect
Reverse sorted arrays cause maximum shifts, leading to worst-case O(n²) time.
Explain how Insertion Sort works step-by-step on an example array.
Think about how you sort playing cards in your hand.
You got /4 concepts.
Describe the time complexity of Insertion Sort and when it performs best and worst.
Consider how many shifts happen in different input orders.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Insertion Sort algorithm?
easy
A. It builds the sorted array one element at a time by inserting elements in their correct position.
B. It divides the array into halves and sorts each half recursively.
C. It repeatedly swaps adjacent elements if they are in the wrong order.
D. It selects the smallest element and swaps it with the first unsorted element.
Solution
Step 1: Understand insertion sort 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
Other options describe merge sort (B), bubble sort (C), and selection sort (D), which are different algorithms.
Final Answer:
It builds the sorted array one element at a time by inserting elements in their 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 selection sort with insertion sort
2. Which of the following is the correct syntax to start the outer loop for insertion sort in C# for an array arr of length n?
easy
A. for (int i = 0; i < n; i++)
B. for (int i = 1; i <= n; i++)
C. for (int i = n - 1; i >= 0; i--)
D. for (int i = 1; i < n; i++)
Solution
Step 1: Identify starting index for insertion sort
Insertion sort starts from the second element (index 1) because the first element is considered sorted.
Step 2: Check loop conditions
The loop runs while i < n to cover all elements. for (int i = 1; i < n; i++) matches this.
Final Answer:
for (int i = 1; i < n; i++) -> Option D
Quick Check:
Start from 1 to n-1 for insertion sort loop [OK]
Hint: Start outer loop at 1, not 0, for insertion sort [OK]
Common Mistakes:
Starting loop at 0 instead of 1
Using <= n instead of < n causing out-of-bounds
Counting backwards incorrectly
3. What will be the output array after applying insertion sort on int[] arr = {4, 3, 2, 1}?
medium
A. [1, 3, 2, 4]
B. [4, 3, 2, 1]
C. [1, 2, 3, 4]
D. [2, 3, 4, 1]
Solution
Step 1: Trace insertion sort on given array
Start with 4 (sorted). Insert 3 before 4 -> [3,4,2,1]. Insert 2 before 3 -> [2,3,4,1]. Insert 1 before 2 -> [1,2,3,4].
Step 2: Confirm final sorted array
The array is sorted in ascending order: [1, 2, 3, 4].