


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This assignment was submitted to Prof. Madhulata Nirmal at Chhattisgarh Swami Vivekanand Technical University for Theory of Complexity and Algorithms course. It includes: Sorting, MergeSort, Variation, QuickSort, Algorithms, Randomized, Sequence, Inversions
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Solutions to Exercises related to Sorting
Note: As you might suspect, these exercises can be solved essentially using a variation of the MergeSort or (randomized) QuickSort algorithms.
Solution: This one is quite easy to do. All we really do is we can start performing the MergeSort or randomized Quicksort algorithm. Suppose we’re using the MergeSort algorithm. Then, as we’re merging the lists (or some of the sublists in one of the recursive calls), we merely check to see if the two items we’re comparing as we merge the lists are equal to each other. If so, we can stop and report “Yes”. If we never find an equal pair of integers, then when we’re finished with the sorting, we report “No”. The same thing can be done with the randomized QuickSort algorithm, but when we’re creating the sublists using the pivot element, if the sublist we called E is ever non-empty at some step, then we again report “Yes”. Otherwise, we do the same thing as above, i.e. as we’re merging the sorted sublists, we check to see if we find a pair of elements that are equal. Using the first method takes time O(n log n) in the worst case as we are basically performing the MergeSort algorithm. The second method takes expected time O(n log n) using the randomized Quick- Sort method.
Solution: The easiest example is the sequence
n, n − 1 , n − 2 ,... , 2 , 1
where everything is out of order. This has
(n 2
) = n(n 2 − 1) = O(n^2 ) inversions. There are many other examples possible like (assuming n is even) n 2
n 2
n 2
n 2
(In other words, reverse, say the first half of the list, but leave the second half in the natural numerical order.) This sequence has
(n/ 2 2
1 2 (^
n 2 )(^
n 2 −^ 1) =^ O(n
(^2) ) inversions.
Solution: Surprise, surprise... Apply the MergeSort algorithm on each list X and Y separately. Then when we’re done with sorting each list, we compare them element by element to see if they are equal. The only difference between the two interpretations is how we might consider merging the sublists. If we want to exclude the duplicates to consider X and Y equal, then we need only include one copy when we merge the lists. In other words, if we have X = (1, 1 , 1 , 1 , 2) and Y = (2, 2 , 1 , 2 , 1), when we sort and “merge” the lists, we can end up with the new lists X′^ = (1, 2) and Y ′^ = (1, 2) which we compare and see they are equal. If we wish to include the duplicates, then when we sort the lists we will end up with (1, 1 , 1 , 1 , 2) and (1, 1 , 2 , 2 , 2) which we then compare element by element and see they are different lists. In any case, depending upon how we wish to compare the lists, we can still do this in time O(n log n), as we apply MergeSort twice (or some small variation of it if we remove the duplicates).
What’s the idea? Pick a pivot element for A, and divide A into the three sets LA, RA, EA as normal. Suppose the value of this pivot el- ement is k 1. Since we’re given the value x, use x − k 1 as the pivot element for B. As we subdivide B into the sets LB , RB , EB , if the set EB is non-empty, we can stop and we’re done with a positive answer. This is because k 1 is in A and we would have just shown that the value x − k 1 is in B.
Otherwise we can continue into the next recursive function calls. We would pick a pivot value for the subset LA. Say this value is k 2. We would then use the value x − k 2 as a pivot value for the set RB. Why? Because, once again, as we subdivide the set RB we will determine if there is element having value x − k 2 in it. If so, we combine this with k 2 to find our solution to x = a + b as desired.
So we can continue in this fashion to determine if there is solution of the form we’re looking for. Note that at some stages we might be able to further short-circuit this search process. If, for example, during the first steps, we find that the set RB (consisting of items that are greater than x − k 1 ) is empty, then we need not consider any elements in LA. (Why?)
Again, since we’re doing the randomized Quicksort algorithm, this whole procedure will run in (expected) time O(n log n).