Problem 1
Consider the following program
Assume we can test that L is sorted time, that we can compute a random permutation of L in time.
P1.1
Does the 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:
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 .
The bound function for the while-loop is as follow:
- The algorithm is probabilistic, and the expected number of iterations is .
- The probability for get the sorted list is .
One can use Bernoulli’s trial to find success probability . The cumulative probability of having sorted the list after attempts is
P1.2
Assume the program 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 permutations before finding the sorted list. The worst case runtime complexity is .
Best case scenario occurs when the first generated permutation is the sorted list, which has the probability of , which has the runtime of 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 . 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 .
This probability of success of each run is , where is the number of elements in the list.
The expected case runtime complexity is , since the expected number of iterations is . (since each permutation will take time to check if array is sorted.
Problem 2
The median of a list of distinct values is the middle value : an equal number of values in are smaller and larger than . For example, in the list , the median is 3. Consider two sorted lists and with distinct values. You may assume that the total number of values in and is odd ( is odd). Hence, there is a value such that an equal amount of other values smaller and larger than .
P2.1
Provide an algorithm
Median(A, B)
that computes the median of the combined list in time.
Solution
P2.2
Explain why your algorithm is correct and why the complexity is .
Solution
The median of combined list is the value such that it either the maximum value of left elements or minimum value of right elements (since is odd). Additionally, it partitions the array such that left side will always contains elements.
Since it employs binary search on two smaller arrays and adjusting the partition , it halves the search space through each iteration to smaller array.
Thus, one can then achieve the complexity (of binary search) as .
P2.3
Let be an algorithm with complexity that computes the middle value . Argue how we can use to break up the Merge-step necessary to merge two sorted lists with values into two independent Merge-steps that each merge only values.
Solution
After using to find median of , given that , median will split the list into two halves, each with elements.
Partition and into two subsets and such that left subsets contains items median, right subsets contains median.
Proceed with two independent Merge-steps that each merge only values for both lower and higher sets. Finally concatenate these two arrays into one sorted lists.
Overall complexity for the merge ops is as each sub-problem involves merging elements.