What is the main 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 already sorted elements.
Click to reveal answer
beginner
How does Insertion Sort handle the elements during sorting?
It divides the list into a sorted and an unsorted part. It takes elements from the unsorted part and inserts them into the correct position in the sorted part.
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. This happens when the list is sorted in reverse order.
Click to reveal answer
intermediate
Why is Insertion Sort efficient for nearly sorted lists?
Because it only moves elements that are out of order, so if the list is almost sorted, fewer moves are needed, making it close to O(n) time.
Click to reveal answer
beginner
Show the first step of Insertion Sort on the list [5, 3, 8, 6]. What does the list look like after inserting the second element?
After inserting 3 into the sorted part [5], the list becomes [3, 5, 8, 6].
Click to reveal answer
What part of the list does Insertion Sort consider sorted at the start?
AThe first element
BThe entire list
CThe last element
DNo elements
✗ Incorrect
Insertion Sort starts with the first element as the sorted part and inserts other elements into it.
Which of these best describes the way Insertion Sort moves elements?
ASwaps adjacent elements repeatedly
BDivides the list and merges sorted parts
CSelects the smallest element and swaps it with the first unsorted element
DInserts elements into their correct position by shifting larger elements right
✗ Incorrect
Insertion Sort shifts larger elements to the right to make space for the element being inserted.
What is the best-case time complexity of Insertion Sort?
AO(n²)
BO(n)
CO(log n)
DO(n log n)
✗ Incorrect
When the list is already sorted, Insertion Sort only makes one pass, resulting in O(n) time.
Which scenario causes Insertion Sort to perform the most operations?
AList is already sorted
BList has all equal elements
CList is sorted in reverse order
DList is randomly ordered
✗ Incorrect
Reverse order requires maximum shifting of elements, causing worst-case O(n²) time.
What is the main advantage of Insertion Sort compared to more complex algorithms?
AIt uses less memory and is simple to implement
BIt is always faster than other algorithms
CIt sorts in parallel
DIt never requires shifting elements
✗ Incorrect
Insertion Sort is simple and uses minimal extra memory, making it good for small or nearly sorted lists.
Explain how Insertion Sort processes a list step-by-step.
Think about dividing the list into sorted and unsorted parts.
You got /6 concepts.
Describe the time complexity of Insertion Sort and when it performs best and worst.
Consider how many shifts are needed in different list orders.
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. Build the sorted array one element at a time by inserting elements into their correct position
C. Swap adjacent elements if they are in wrong order repeatedly
D. Select the smallest element and swap it with the first unsorted element
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 relative to the already sorted part.
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:
Build the sorted array one element at a time by inserting elements into their correct position -> Option B
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 or selection sort
Thinking insertion sort divides array recursively
Assuming insertion sort swaps all elements repeatedly
2. Which of the following Go code snippets correctly starts the outer loop for insertion sort on a slice arr?
easy
A. for i := 1; i < len(arr); i++ {
B. for i := 0; i < len(arr); i++ {
C. for i := len(arr)-1; i >= 0; i-- {
D. for i := 1; i <= len(arr); i++ {
Solution
Step 1: Identify correct loop start for insertion sort
Insertion sort starts from the second element (index 1) because the first element is considered sorted.
Step 2: Check loop conditions
Loop runs while i is less than length of array. for i := 1; i < len(arr); i++ { matches this exactly.
Final Answer:
for i := 1; i < len(arr); i++ { -> Option A
Quick Check:
Start from index 1 to end [OK]
Hint: Start outer loop at 1, not 0, for insertion sort [OK]
Common Mistakes:
Starting loop at 0 instead of 1
Using <= instead of < in loop condition
Looping backwards incorrectly
3. What will be the output slice after running insertion sort on arr := []int{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 slice
The array is sorted in ascending order: [1, 2, 3, 4].