Answer Key AAD
Answer Key AAD
2) Solve the following recurrence using the iteration method. 𝑻(𝒏) = 𝟑 ∗ 𝑻(𝒏 − 𝟏) +
𝟏 𝒂𝒏𝒅 𝑻 (𝟏) = 𝟏 [4 pts]
9 15 27 46 65 78 82 94 112 139
15 8 6 42 25 36 13
Pass 1:
1. Compare 15 and 8: 15 > 8, swap. Array: [8, 15, 6, 42, 25, 36, 13]
2. Compare 15 and 6: 15 > 6, swap. Array: [8, 6, 15, 42, 25, 36, 13]
3. Compare 15 and 42: 15 < 42, no swap. Array: [8, 6, 15, 42, 25, 36, 13]
4. Compare 42 and 25: 42 > 25, swap. Array: [8, 6, 15, 25, 42, 36, 13]
5. Compare 42 and 36: 42 > 36, swap. Array: [8, 6, 15, 25, 36, 42, 13]
6. Compare 42 and 13: 42 > 13, swap. Array: [8, 6, 15, 25, 36, 13, 42]
Pass 2:
1. Compare 8 and 6: 8 > 6, swap. Array: [6, 8, 15, 25, 36, 13, 42]
2. Compare 8 and 15: 8 < 15, no swap. Array: [6, 8, 15, 25, 36, 13, 42]
3. Compare 15 and 25: 15 < 25, no swap. Array: [6, 8, 15, 25, 36, 13, 42]
4. Compare 25 and 36: 25 < 36, no swap. Array: [6, 8, 15, 25, 36, 13, 42]
5. Compare 36 and 13: 36 > 13, swap. Array: [6, 8, 15, 25, 13, 36, 42]
Pass 3:
1. Compare 6 and 8: 6 < 8, no swap. Array: [6, 8, 15, 25, 13, 36, 42]
2. Compare 8 and 15: 8 < 15, no swap. Array: [6, 8, 15, 25, 13, 36, 42]
3. Compare 15 and 25: 15 < 25, no swap. Array: [6, 8, 15, 25, 13, 36, 42]
4. Compare 25 and 13: 25 > 13, swap. Array: [6, 8, 15, 13, 25, 36, 42]
Pass 4:
1. Compare 6 and 8: 6 < 8, no swap. Array: [6, 8, 15, 13, 25, 36, 42]
2. Compare 8 and 15: 8 < 15, no swap. Array: [6, 8, 15, 13, 25, 36, 42]
3. Compare 15 and 13: 15 > 13, swap. Array: [6, 8, 13, 15, 25, 36, 42]
Pass 5:
1. Compare 6 and 8: 6 < 8, no swap. Array: [6, 8, 13, 15, 25, 36, 42]
2. Compare 8 and 13: 8 < 13, no swap. Array: [6, 8, 13, 15, 25, 36, 42]
Pass 6:
1. Compare 6 and 8: 6 < 8, no swap. Array: [6, 8, 13, 15, 25, 36, 42]
Result: The sorted array is [6, 8, 13, 15, 25, 36, 42].
32 18 6 23 58 18 44
1. Choose Gaps:
o We'll use a gap sequence of n/2, n/4, ..., 1.
o The array has 7 elements, so the initial gap is 7/2 = 3.
2. Sort with Gap 3:
o Compare and swap elements at indices 0 and 3 (32 and 23): [23, 18, 6, 32,
58, 18, 44]
oCompare and swap elements at indices 1 and 4 (18 and 58): [23, 18, 6, 32,
58, 18, 44] (no swap)
o Compare and swap elements at indices 2 and 5 (6 and 18): [23, 18, 6, 32,
58, 18, 44] (no swap)
o Compare and swap elements at indices 3 and 6 (32 and 44): [23, 18, 6, 32,
58, 18, 44] (no swap)
o Now, we do insertion sort with the gap.
o Index 3, we have 32. 23 is less, so we swap.
o Index 4, 58. No swap.
o Index 5, 18. No Swap.
o Index 6, 44. No Swap.
3. Reduce Gap to 3/2 = 1:
o The next gap is 3/2 = 1 (integer division). This is essentially insertion sort.
4. Sort with Gap 1 (Insertion Sort):
o Compare and swap elements at indices 1 and 0 (18 and 23): [18, 23, 6, 32,
58, 18, 44]
o Compare and swap elements at indices 2 and 1 (6 and 23): [18, 6, 23, 32,
58, 18, 44]
o Compare and swap elements at indices 1 and 0 (6 and 18): [6, 18, 23, 32,
58, 18, 44]
o Compare and swap elements at indices 3 and 2 (32 and 23): [6, 18, 23, 32,
58, 18, 44] (no swap)
o Compare and swap elements at indices 4 and 3 (58 and 32): [6, 18, 23, 32,
58, 18, 44] (no swap)
o Compare and swap elements at indices 5 and 4 (18 and 58): [6, 18, 23, 32,
18, 58, 44]
o Compare and swap elements at indices 4 and 3 (18 and 32): [6, 18, 23, 18,
32, 58, 44]
o Compare and swap elements at indices 3 and 2 (18 and 23): [6, 18, 18, 23,
32, 58, 44]
o Compare and swap elements at indices 6 and 5 (44 and 58): [6, 18, 18, 23,
32, 44, 58]
Result:
The sorted array is [6, 18, 18, 23, 32, 44, 58].