Computer Science – 19.1 Algorithms | e-Consult
19.1 Algorithms (1 questions)
Login to see all questions.
Click on a question to view the answer
Step-by-step walkthrough of Insertion Sort:
| Iteration | List State |
| 1 (Insert 2 at index 1) | [5, 2, 9, 1, 5, 6] (2 is inserted at index 1) |
| 2 (Insert 9 at index 2) | [5, 2, 9, 1, 5, 6] (9 is inserted at index 2) |
| 3 (Insert 1 at index 3) | [5, 2, 9, 1, 5, 6] (1 is inserted at index 3) |
| 4 (Insert 5 at index 4) | [5, 2, 9, 1, 5, 6] (5 is inserted at index 4) |
| 5 (Insert 6 at index 5) | [2, 5, 5, 9, 1, 6] (6 is inserted at index 5) |
Explanation:
- Iteration 1: The element 2 is picked. It's compared with 5. Since 2 is less than 5, 5 is shifted to the right, and 2 is inserted at index 1.
- Iteration 2: The element 9 is picked. It's compared with 5. Since 9 is greater than 5, 5 is shifted to the right, and 9 is inserted at index 2.
- Iteration 3: The element 1 is picked. It's compared with 9. Since 1 is less than 9, 9 is shifted to the right, and 1 is inserted at index 3.
- Iteration 4: The element 5 is picked. It's compared with 1. Since 5 is greater than 1, 1 is shifted to the right, and 5 is inserted at index 4.
- Iteration 5: The element 6 is picked. It's compared with 5. Since 6 is greater than 5, 5 is shifted to the right, and 6 is inserted at index 5.