Sample Mid Term For Review: 2. A. Iteration (S)
Sample Mid Term For Review: 2. A. Iteration (S)
Sample Mid Term For Review: 2. A. Iteration (S)
1.
a. Show that n2 + 2 is not O(n) [do it in class]
b. Let f(n) = (lg n)^k and g(n) = . Find the relationship between f and g considering
asymptotic growth (Sample Medium to hard type of question) [do it in class]
2.
a. Iteration (s)
Ans.
We want T(1). So we let n-k = 1. Solving for k, we get k = n - 1. Now plug back in.
Anoher Iteration -
Ans. Do it in class
3.
b. Other sorting
element of the array [1, 5, 23, 0, 8, 4]. Assume that the rightmost element is used as the pivot in
each case. Show what happens in each self-call, indicating the new input array and the current value
of k.
QS (5, 23, 8) , k = 1
k=1
QS (5), k = 1
L = [ ], E = [5], G = [ ]
4.
Theorem 1. Every comparison based sorting algorithm has, for each n, running on input of size n, a
worst case in which its running time is Ω(nlog n).
b. Sorting
Insertion Sort
O(N2) / Yes / Yes
Bubble Sort
O(N2) / Depends / Yes
Heap Sort
O(N log N) [ Later]
Merge Sort
O(N log N) / Yes / No
Quick Sort
O(N2) / No / If Partition is in Place
Binary Search
O(N) - Later
Radix Sort
O(N) / Yes / No
Counting Sort
O(N) / Yes / No
c. Amortization
A twisty table is a kind of data structure that stores Strings and that uses an array in the background
(assume it is already initialized and has as many available slots as necessary without resizing) and
that has three primary operations:
• AddOne(x) – adds String x to the background array in the next available slot
• AddThree(x,y,z) – adds Strings x, y, and z to the background array by placing them in the
next three available slots.
• ClearAll() – removes all Strings currently in the array by setting them to null.
Show that the amortized running time of ClearAll is O(1). Do the steps required by filling in the
following table. Hint: For your amortized cost function, try charging 2 cyberdollars for AddOne, 6
cyberdollars for AddThree and 0 cyberdollars for ClearAll.
The cost function c:
c(AddOne) = 1
c(AddThree) = 3
array
Show that your amortized cost function never results in negative amortized profit:
If AddOne is executed k times and AddThree is executed m times, the profit will be k + 3m,
which is enough to pay for a ClearAll operation, which would cost k + 3m.