Q.Consider a list of 10 elements: Array = [7,11,3,10,17,23,1,4,21,5] Determine the partially sorted list after three complete passes of insertion sort.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →After three complete passes of insertion sort on the given array, the first four elements will be sorted among themselves while the rest remain untouched.
Insertion sort builds the sorted portion of the array one element at a time. It picks each element in turn and inserts it into its correct position within the already-sorted portion to its left. This is fundamentally different from bubble sort or selection sort — insertion sort grows a sorted prefix.
How Insertion Sort Works
The algorithm conceptually divides the array into two parts: a sorted left portion and an unsorted right portion. Initially, the sorted portion contains just the first element (a single element is trivially sorted). In each pass, we take the next element from the unsorted portion and insert it into the correct position in the sorted portion by shifting larger elements to the right.
Pass numbering: The first pass processes the element at index 1 (the second element), the second pass processes index 2, and so on. After passes, the first elements are sorted among themselves.
Step-by-Step Trace
Let's trace three complete passes on Array = [7, 11, 3, 10, 17, 23, 1, 4, 21, 5].
| Pass | Element to Insert | Action | Array State After Pass |
|---|---|---|---|
| Initial | — | — | [7, 11, 3, 10, 17, 23, 1, 4, 21, 5] |
| 1 | 11 (index 1) | Compare with 7. Since 11 > 7, it stays in place. | [7, 11, 3, 10, 17, 23, 1, 4, 21, 5] |
| 2 | 3 (index 2) | Compare with 11 → shift 11 right. Compare with 7 → shift 7 right. Insert 3 at index 0. | [3, 7, 11, 10, 17, 23, 1, 4, 21, 5] |
| 3 | 10 (index 3) | Compare with 11 → shift 11 right. Compare with 7 → 10 > 7, so insert 10 between 7 and 11. | [3, 7, 10, 11, 17, 23, 1, 4, 21, 5] |
Detailed Explanation of Each Pass
Pass 1: We consider element at index 1, which is 11. The sorted portion is [7]. Since 11 > 7, no shifting is needed; 11 remains at index 1.
Pass 2: We consider element at index 2, which is 3. The sorted portion is [7, 11]. We compare 3 with 11 — it's smaller, so 11 shifts to index 2. Then compare 3 with 7 — it's smaller, so 7 shifts to index 1. Finally, 3 is inserted at index 0.
Pass 3: We consider element at index 3, which is 10. The sorted portion is now [3, 7, 11]. We compare 10 with 11 — it's smaller, so 11 shifts to index 3. Then compare 10 with 7 — it's larger, so we stop. Insert 10 at index 2. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.