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 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 perform on nearly sorted data?
Insertion Sort performs very efficiently on nearly sorted data, with a time complexity close to O(n), because fewer elements need to be moved.
Click to reveal answer
beginner
In Insertion Sort, what happens during the inner loop?
During the inner loop, the current element is compared with elements in the sorted part and shifted to the right until the correct position for the current element is found.
Click to reveal answer
beginner
Write the basic steps of Insertion Sort in simple words.
1. Start from the second element. 2. Compare it with elements before it. 3. Shift larger elements one position to the right. 4. Insert the current element in the correct position. 5. Repeat for all elements.
Click to reveal answer
What is the best case time complexity of Insertion Sort?
AO(log n)
BO(n log n)
CO(n²)
DO(n)
✗ Incorrect
The best case occurs when the array is already sorted, so Insertion Sort only makes one pass with no shifts, resulting in O(n) time.
Which part of the array is always sorted during Insertion Sort?
AThe right side
BThe entire array at once
CThe left side
DThe middle
✗ Incorrect
Insertion Sort builds the sorted part from the left side, growing it one element at a time.
What does Insertion Sort do when it finds an element smaller than the current element during comparison?
AShifts the larger element to the right
BSwaps the elements immediately
CStops and inserts the element
DIgnores the element
✗ Incorrect
It shifts the larger elements to the right to make space for the current element to be inserted.
Which of these is true about Insertion Sort?
AIt is a divide and conquer algorithm
BIt is stable and in-place
CIt requires extra memory
DIt is the fastest sorting algorithm for large data
✗ Incorrect
Insertion Sort is stable (does not change the order of equal elements) and sorts the array in-place without extra memory.
What is the first step in Insertion Sort?
APick the second element
BPick the first element
CPick the last element
DPick the middle element
✗ Incorrect
Insertion Sort starts from the second element and inserts it into the sorted part on the left.
Explain how Insertion Sort works step-by-step using a simple example.
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 in different input orders.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Insertion Sort algorithm?
easy
A. Select the smallest element and swap it with the first unsorted element
B. Divide the array into halves and sort each half separately
C. Swap adjacent elements if they are in the wrong order repeatedly
D. Build the sorted array one element at a time by inserting elements in the correct position
Solution
Step 1: Understand insertion sort process
Insertion sort builds the sorted array 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 sorting algorithms: divide and conquer (merge sort), swapping adjacent elements (bubble sort), selecting smallest element (selection sort).
Final Answer:
Build the sorted array one element at a time by inserting elements in the correct position -> Option D
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 into halves
Mixing insertion sort with selection sort
2. Which of the following is the correct syntax to start the outer loop for insertion sort in Java for an array arr of length n?
easy
A. for (int i = 0; i < n; i++)
B. for (int i = n - 1; i >= 0; i--)
C. for (int i = 1; i < n; i++)
D. for (int i = 1; i <= n; i++)
Solution
Step 1: Identify insertion sort loop start
Insertion sort starts from the second element (index 1) because the first element is considered sorted.
Step 2: Check loop conditions
The loop runs from i = 1 up to i < n (not i <= n) to avoid index out of bounds.
Final Answer:
for (int i = 1; i < n; i++) -> Option C
Quick Check:
Start from second element: i=1 [OK]
Hint: Start outer loop at 1, not 0, in insertion sort [OK]
Common Mistakes:
Starting loop at 0 instead of 1
Using i <= n causing index errors
Looping backwards incorrectly
3. What will be the output array after applying insertion sort on int[] arr = {4, 3, 2, 1};?
4. Consider this insertion sort code snippet in Java:
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j] = key;
}
What is the error in this code?
medium
A. The assignment should be arr[j + 1] = key; instead of arr[j] = key;
B. The loop should start from i = 0 instead of 1
C. The condition in while loop should be arr[j] < key
D. The decrement j-- should be j++
Solution
Step 1: Analyze where key is placed after shifting
After shifting elements greater than key to the right, key should be placed at arr[j + 1] because j was decremented one extra time.
Step 2: Identify incorrect assignment
The code assigns arr[j] = key; which overwrites the wrong index. Correct is arr[j + 1] = key;.
Final Answer:
The assignment should be arr[j + 1] = key; instead of arr[j] = key; -> Option A
Quick Check:
Place key at arr[j+1] after shifting [OK]
Hint: Place key at arr[j+1] after shifting elements [OK]
Common Mistakes:
Assigning key to arr[j] instead of arr[j+1]
Starting loop at wrong index
Wrong while condition direction
5. You have an array int[] arr = {5, 3, 8, 6, 2};. After performing insertion sort, what will be the state of the array after the third iteration of the outer loop?