1) Let f(n)=8n2 + 5n + 1, and g(n)=n2.
Prove that f(n)= (n2) [4 pts]
We are given: f(n)=8n2 + 5n + 1 and g(n)=n2
Thus, we need to show: C1n2≤8n2+5n+1≤C2n2
To find upper bound 8n2+5n+1≤C2n2 Dividing both sides by n2 gives 8+5/n+1/n2≤ C2
So C2=8+5+1=14 for all n≥1 This proves the upper bound
To find upper bound C1n2≤8n2+5n+1 Dividing both sides by n2 gives C1≤8+5/n+1/n2
So C1=8 for all n≥1 This proves the lower bound
Finally, 8n2≤8n2+5n+1≤14n2 for all n≥1 with Constants C1=8 and C2=14 proves f(n)= (n2)
2) Solve the following recurrence using the iteration method. 𝑻(𝒏) = 𝟑 ∗ 𝑻(𝒏 − 𝟏) +
𝟏 𝒂𝒏𝒅 𝑻 (𝟏) = 𝟏 [4 pts]
We repeatedly substitute T(n−1), T(n−2), and so on:
• T(n)=3*T(n−1) +1
• T(n−1) = 3*T(n−2) +1
• T(n−2) = 3*T(n−3) +1
• T(n−3) =3*T(n−4) +1
• ……..
Substitute T(n−1) into the original recurrence:
T(n)=3(3*T(n−2) +1) +1=32T(n−2) +3+1
Now, substitute T(n−2):
T(n)=32(3*T(n−3) +1) +3+1=33T(n−3) +32+3+1
Continuing the pattern, after k substitutions, we have:
T(n)=3k*T(n−k) +3k−1+3k−2+...+31+1
𝑘−1
T(n)=3k*T(n−k)+∑𝑖=0 3𝑖
We stop expanding when n−k=1, meaning k=n−1. Using the base case T (1) =1:
T(n−(n−1)) =T (1) =1
𝑛−2
So, T(n)=3n-1+∑𝑖=0 3𝑖
𝑛−2 𝑖
The sum is Geometric Series ∑ 3 is equivalent with (3n-1 -1)/2
𝑖=0
𝑛−2
T(n)=3n-1+∑𝑖=0 3𝑖 =3n-1+(3n-1 -1)/2
T(n)=2 ((3n-1)+ (3n-1 -1))/2
T(n)=(3n-1)/2
3) Using given array below find 112 using Exponential Search [5 pts]
9 15 27 46 65 78 82 94 112 139
➢ Find the Range:
• Start with bound = 1.
• While bound is less than the array length and array[bound] is less than or equal to the
target, double bound.
o bound = 1, array[1] = 15 <= 112, so bound = 2.
o bound = 2, array[2] = 27 <= 112, so bound = 4.
o bound = 4, array[4] = 65 <= 112, so bound = 8.
o bound = 8, array[8] = 112 <= 112, so bound = 16.
o bound = 16, but 16 is greater than the array length which is 10, so we stop.
• Now, we know that if the target is in the array, it must be in the range array[bound/2 ...
min(bound, length-1)]. In our case, bound/2 = 4, and min(bound, length-1) = min(16, 9) = 9.
• So, the range is array[4...9], which is [65, 78, 82, 94, 112, 139].
➢ Perform Binary Search:
• Now, we perform a binary search within the identified range.
• low = 4
• high = 9
• mid = (low + high) // 2 = (4 + 9) // 2 = 6
• array[mid] = array[6] = 82
• 82 < 112, so low = mid + 1 = 7.
• low = 7
• high = 9
• mid = (7 + 9) // 2 = 8
• array[mid] = array[8] = 112
• array[mid] == 112, so we found the target at index 8.
4) Sort the following table using Bubble Sort [6 pts]
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].
5) Sort the following table using Shell Sort [6 pts]
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].