Assume we can test that L is sorted Θ(∣L∣) time, that we can compute a random permutation of L in Θ(∣L∣) time.
P1.1
Does the SORT program sort correctly? If yes, then provide an invariant for the while-loop and provide
a bound function that can be used to prove the correctness of the program. If no, then argue why the
program is not correct.
Solution
The program does sort correctly, but inefficiently, given enough time. This is known as Bogosort, as there are finite permutation in the list, one of them are the sorted array.
The invariant for the while-loop is as follow:
Invariant: 0<i<S∣Li is a permutation of L0∧S=n!
This invariant holds through the while-loop because the permutation of the list is a permutation of the original list, and the number of permutations is n!.
The bound function for the while-loop is as follow:
The algorithm is probabilistic, and the expected number of iterations is n!.
The probability for get the sorted list is p=n!1.
One can use Bernoulli’s trial to find success probability p. The cumulative probability of having sorted the list after k attempts is 1−(1−n!1)k
P1.2
Assume the program SORT is correct. Is the program stable? Explain why.
Solution
The program is not stable since SORT is non-deterministic.
Each iteration generates a random permutation of the list. If the list contains duplicate elements, their relative order is then not reserved between permutations.
The algorithm does not consider the original order of elements when determine the list is sorted.
P1.3
What is the worst case runtime complexity of this program? What is the best case runtime complexity of this program? Is this program optimal? Explain your arguments.
Solution
Worst case scenario occurs when the algorithm goes through all n! permutations before finding the sorted list. The worst case runtime complexity is Θ(n!⋅n).
Best case scenario occurs when the first generated permutation is the sorted list, which has the probability of n!1, which has the runtime of Θ(n) as it only needs to generate one random permutation and check for sorted.
No, the program is not optimal, since it is based on random permutation, and the expected number of iterations is n!. There are no guarantee that the algorithm is reliable to sort the list. Definitely not as optimal as other sorting algorithm such as merge-sort or heap-sort.
P1.4
What is the expected case runtime complexity of this program? Explain your answer.
Solution
Similar to previous mentioned, one can use Bernoulli’s trial to find success probability p.
This probability p of success of each run is n!1, where n is the number of elements in the list.
The expected case runtime complexity is Θ(n!⋅n), since the expected number of iterations is n!. (since each permutation will take Θ(n) time to check if array is sorted.
Problem 2
The median of a list L of distinct values is the middle value v∈L: an equal number of values in L are smaller and larger than v. For example, in the list L=[1,5,4,2,3], the median is 3. Consider two sorted lists A[0…N) and B[0…M) with N+M distinct values. You may assume that the total number of values in A and B is odd (N+M is odd). Hence, there is a value v∈(A∪B such that an equal amount E=[2N+M] of other values smaller and larger than v.
P2.1
Provide an algorithm Median(A, B) that computes the median of the combined list A∪B in O(log2(N+M)) time.
Explain why your algorithm is correct and why the complexity is Θ(log2(N+M)).
Solution
The median of combined list A∪B is the value v such that it either the maximum value of left elements or minimum value of right elements (since N+M is odd). Additionally, it partitions the array such that left side will always contains ⌊2M+N⌋ elements.
Since it employs binary search on two smaller arrays and adjusting the partition A[i−1],A[i],B[j−1],B[j], it halves the search space through each iteration to smaller array.
Thus, one can then achieve the complexity (of binary search) as Θ(log2(N+M)).
P2.3
Let P be an algorithm with complexity Θ(log2(N+M)) that computes the middle value A∪B. Argue how we can use P to break up the Merge-step necessary to merge two sorted lists with N+M=2E+1 values into two independent Merge-steps that each merge only E values.
Solution
After using P to find median of A∪B, given that N+M=2E+1, median will split the list into two halves, each with E elements.
Partition A and B into two subsets Aleft,Aright and Bleft,Bright such that left subsets contains items ≤ median, right subsets contains gee median.
Proceed with two independent Merge-steps that each merge only E values for both lower and higher sets. Finally concatenate these two arrays into one sorted lists.
Overall complexity for the merge ops is O(2E) as each sub-problem involves merging E elements.