Untitled
Untitled
Its worst-case running t ime has a lower order of growth than insertion sort. Since we are dealing with s ubproblems, we state each subproblem as sorting a subarray A[p .. r]. Initially, p = 1 and r = n, but these values change as we recurse through subproblems. To sort A[p .. r]: 1. Divide Step If a given array A has zero or one element, simply return; it is already sorted. Otherwise, split A[p .. r] into two subarrays A[p .. q] and A[q + 1 .. r], each containing about half of the elements of A[p .. r]. That is, q is the halfway p oint of A[p .. r]. 2. Conquer Step Conquer by recursively sorting the two subarrays A[p .. q] and A[q + 1 .. r]. 3. Combine Step Combine the elements back in A[p .. r] by merging the two sorted subarrays A[p . . q] and A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure MERGE (A, p, q, r). Note that the recursion bottoms out when the subarray has just one element, so t hat it is trivially sorted. Algorithm: Merge Sort To sort the entire sequence A[1 .. n], make the initial call to the procedure M ERGE-SORT (A, 1, n). MERGE-SORT (A, p, r) 1. IF p < r // Check for base case 2. THEN q = FLOOR[(p + r)/2] // Divide step 3. MERGE (A, p, q) // Conquer step. 4. MERGE (A, q + 1, r) // Conquer step. 5. MERGE (A, p, q, r) // Conquer step. Example: Bottom-up view of the above procedure for n = 8. Merging What remains is the MERGE procedure. The following is the input and output of th e MERGE procedure. INPUT: Array A and indices p, q, r such that p q r and subarray A[p .. q] is sor ted and subarray A[q + 1 .. r] is sorted. By restrictions on p, q, r, neither su barray is empty. OUTPUT: The two subarrays are merged into a single sorted subarray in A[p .. r]. We implement it so that it takes (n) time, where n = r p + 1, which is the number of elements being merged. Idea Behind Linear Time Merging Think of two piles of cards, Each pile is sorted and placed face up on a table w ith the smallest cards on top. We will merge these into a single sorted pile, fa ce down on the table. A basic step: Choose the smaller of the two top cards. Remove it from its pile, thereby exposing a new top card. Place the chosen card face down onto the output pile. Repeatedly perform basic steps until one input pile is empty. Once one input pile empties, just take the remaining input pile and place it fac e down onto the output pile. Each basic step should take constant time, since we check just the two top cards . There are at most n basic steps, since each basic step removes one card from t he input piles, and we started with n cards in the input piles. Therefore, this
procedure should take (n) time. Now the question is do we actually need to check whether a pile is empty before each basic step? The answer is no, we do not. Put on the bottom of each input pile a special sent inel card. It contains a special value that we use to simplify the code. We use , since that's guaranteed to lose to any other value. The only way that cannot lo se is when both piles have exposed as their top cards. But when that happens, al l the nonsentinel cards have already been placed into the output pile. We know i n advance that there are exactly r p + 1 nonsentinel cards so stop once we have performed r p + 1 basic steps. Never a need to check for sentinels, since they w ill always lose. Rather than even counting basic steps, just fill up the output array from index p up through and including index r . The pseudocode of the MERGE procedure is as follow: MERGE (A, p, q, r ) 1. n1 q p + 1 2. n2 r q 3. Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1] 4. FOR i 1 TO n1 5. DO L[i] A[p + i 1] 6. FOR j 1 TO n2 7. DO R[j] A[q + j ] 8. L[n1 + 1] 9. R[n2 + 1] 10. i 1 11. j 1 12. FOR k p TO r 13. DO IF L[i ] R[ j] 14. THEN A[k] L[i] 15. i i + 1 o create a document