Select Page

In some situations, we do not require an array to be fully sorted, but just approximately” sorted. There are many ways this can be de ned. In this question, we consider one particular measure concerning the number of pairs of elements that are wildly” out of order.

In a fully sorted array A[1::n], whenever i < j we must have A[i] A[j]. In other words, there are no pairs of elements where i < j but A[i] > A[j]. We relax this de nition a bit; for example, we can allow A[i] to be a little bit” larger than A[j] but not too much. Speci cally, given an array A[1::n], a pair of elements A[i] and A[j] is said to be a bad pair if i < j and A[i] > A[j] + 2. In other words, the earlier” element is larger than the later element by 3 or more. For example, in [1, 5, 2, 3, 10, 9, 8, 4] there are four bad pairs: (5, 2), (10, 4), (9, 4) and (8, 4). We may accept an array to be approximately sorted” if there are no bad pairs. This is a weaker notion than being fully sorted: an array may have no bad pairs but is still not fully sorted, for example [1, 2, 4, 3, 5, 8, 6, 7].

But before we consider how to approximately sort an array (which is not what most of this question is about), we consider the problem of identifying or counting these bad pairs.

1. What is the minimum and maximum number of bad pairs an n-element array can have? [5 marks + 5 marks]

(b) Give a straightforward O(n2) time algorithm to output all bad pairs.                          [10 marks]

1. If we are only interested in the number of bad pairs (without listing them all), we can do better than (b). First, consider a special case in which the array A is such that the rst half A[1::n=2] is fully sorted, the second half A[n=2 + 1::n] is fully sorted, but the whole array may not be fully sorted. An example is A = [1; 3; 6; 7; 2; 4; 5; 8]. Give an O(n) time algorithm to report the number of bad pairs in such an array A.

(Hint: in this special case, a bad pair can only happen when the two numbers involved

are in di erent halves. Also note that you are only required to count the number of bad

pairs. Do not attempt to output all bad pairs.)                                                                      [20 marks]

1. Give an O(n log n) time algorithm to report the number of bad pairs in an array A (that may not have the special property in (c)). You can assume n is a power of 2.

(Hint: Use divide and conquer and modify merge sort, so that it counts the number of bad pairs while sorting the array at the same time. Make use of part (c).) [20 marks]

1. A student Alice said the problem of counting the number of bad pairs has a lower bound of  (n log n). Here is her argument:

As we see in (c) and (d), counting the number of bad pairs requires sorting the array. Since sorting takes  (n log n) time, this problem also requires at least this amount of time.”

Is her argument correct? Explain your answer.                                             [5 marks + 5 marks]

1. (Bonus part) Note that all the above is about counting the number of bad pairs, not approximately sorting an array. Unfortunately, sorting an array approximately (to remove all bad pairs) is not easier than sorting an array fully. Prove an  (n log n) lower bound on the time complexity of this problem.

(Note: this requires some ideas not explicitly spelt out in the lecture notes (hence bonus part). Just dumping all the things in the lecture notes will give you precisely zero marks.)

[+5 bonus marks]

Welcome to one of the bestassignmenthelpcompanies  online .

·         Do you want to order for a customized assignment help task?

·          Click on the order now button

·         Set up your topic, Fix the number of pages, Fix your Order instructions