# Merge Quick Sort

date post

25-Oct-2014Category

## Documents

view

182download

0

Embed Size (px)

### Transcript of Merge Quick Sort

Design and Analysis of Algorithms Chapter 41 Divide and Conquer (I) Dr. Ying Lu [email protected] CSCE 310: Data Structures & Algorithms Design and Analysis of Algorithms Chapter 42 Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbooks companion website http://www.aw-bc.com/info/levitin Some examples and slides are based on lecture notes created by Dr. Ben Choi, Louisiana Technical University and Dr. Chuck Cusack, Hope College I have modified many of their slides and added new slides. CSCE 310: Data Structures & Algorithms Design and Analysis of Algorithms Chapter 43 Divide and Conquer The most well known algorithm design strategy: 1.Divide instance of problem into two or more smaller instances 1. Solve smaller instances recursively 1. Obtain solution to original (larger) instance by combining these solutions Design and Analysis of Algorithms Chapter 44 Divide-and-conquer Technique subproblem 2of size n/2 subproblem 1of size n/2 a solution tosubproblem 1 a solution to the original problem a solution tosubproblem 2 a problem of size n Design and Analysis of Algorithms Chapter 45 Divide and Conquer Examples Sorting: mergesort and quicksort Tree traversals Binary search Matrix multiplication-Strassens algorithm Convex hull-QuickHull algorithm Design and Analysis of Algorithms Chapter 46 Mergesort Algorithm: Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] Sort arrays B and C Merge sorted arrays B and C into array A Design and Analysis of Algorithms Chapter 47 Using Divide and Conquer: Mergesort Mergesort Strategy Sorted Merge Sorted Sorted Sort recursively by Mergesort Sort recursively by Mergesort first last (first + last)/2 Design and Analysis of Algorithms Chapter 48 Mergesort Algorithm: Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] Sort arrays B and C Merge sorted arrays B and C into array A Design and Analysis of Algorithms Chapter 49 Mergesort Algorithm: Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] Sort arrays B and C Merge sorted arrays B and C into array A as follows: Repeat the following until no elements remain in one of the arrays: compare the first elements in the remaining unprocessed portions of the arrays copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A. Design and Analysis of Algorithms Chapter 410 Algorithm: Mergesort Input: Array E and indices first and last, such that the elements E[i] are defined for first bdT(n) (nlog b a) Please refer to Appendix B (P483) for the proof Design and Analysis of Algorithms Chapter 419 In-class exercise Page 128: Problem 5 5. Find the order of growth for solutions of the following recurrences. a. T(n) = 4T(n/2) + n, T(1) = 1 b. T(n) = 4T(n/2) + n22, T(1) = 1 c. T(n) = 4T(n/2) + n33, T(1) = 1 In-Class Exercise 4.1.9 Let A[0, n-1] be an array of n distinct real numbers. A pair (A[i], A[j]) is said to be an inversion if these numbers are out of order, i.e., iA[j]. Design an O(nlogn) algorithm for counting the number of inversions.Design and Analysis of Algorithms Chapter 420 Design and Analysis of Algorithms Chapter 421 Quicksort by Hoare (1962) Select a pivot (partitioning element) Rearrange the list so that all the elements in the positions before the pivot are smaller than or equal to the pivot and those after the pivot are larger than or equal to the pivot Exchange the pivot with the last element in the first (i.e., ) sublist the pivot is now in its final position Sort the two sublists recursively p A[i]p A[i]>p Design and Analysis of Algorithms Chapter 422 Quicksort by Hoare (1962) Select a pivot (partitioning element) Rearrange the list so that all the elements in the positions before the pivot are smaller than or equal to the pivot and those after the pivot are larger than or equal to the pivot Exchange the pivot with the last element in the first (i.e., ) sublist the pivot is now in its final position Sort the two sublists recursively p A[i]p A[i]>p Design and Analysis of Algorithms Chapter 423 The partition algorithm s Design and Analysis of Algorithms Chapter 424 The partition algorithm s A sentinel at A[n] to prevent i advances beyond position n.Design and Analysis of Algorithms Chapter 425 Quicksort Example Recursive implementation with the left most array entry selected as the pivot element. Design and Analysis of Algorithms Chapter 426 Quicksort Animated Example: http://math.hws.edu/TMCM/java/xSortLab/index.html Design and Analysis of Algorithms Chapter 427 Quicksort Algorithm Input: Array E and indices first, and last, s.t. elements E[i] are defined for first s i s last Ouput: E[first], , E[last] is a sorted rearrangement of the array Void quickSort(Element[] E, int first, int last) if (first < last) Element pivotElement = E[first]; int splitPoint = partition(E, pivotElement, first, last); quickSort (E, first, splitPoint 1 ); quickSort (E, splitPoint +1, last ); return; Design and Analysis of Algorithms Chapter 428 Quicksort Analysis Partition can be done in O(n) time, where n is the size of the array Let T(n) be the number of compares required by Quicksort If the pivot ends up at position k, then we have T(n) =T(nk) + T(k 1) + n To determine best-, worst-, and average-case complexity we need to determine the values of k that correspond to these cases. Design and Analysis of Algorithms Chapter 429 Best-Case Complexity The best case is clearly when the pivot always partitions the array equally. Intuitively, this would lead to a recursive depth of at most lg n calls We can actually prove this.In this case T(n) s T(n/2) + T(n/2) + n O(n lg n) Design and Analysis of Algorithms Chapter 430 Worst-Case and Average-Case Complexity The worst-case is when the pivot always ends up in the first or last element.That is, partitions the array as unequally as possible. In this case T(n) = T(n1) + T(11) + n = T(n1) + n = n + (n1) + + 1 = n(n + 1)/2 O(n2) Average case is rather complex, but is where the algorithm earns its name. The bottom line is: ) lg ( lg 386 . 1 ) ( n n n n n A O ~Design and Analysis of Algorithms Chapter 431 Summary of quicksort Best case: split in the middle ( n log n) Worst case: sorted array! ( n2) Average case: random arrays ( n log n) Improvements: better pivot selection: median of three partitioning avoids worst case in sorted files switch to insertion sort on small subfiles elimination of recursion these combine to 20-25% improvement Considered the method of choice for internal sorting for large files (n 10000) In-class exercises 4.2.1 Apply quicksort to sort the list E, X, A, M, P, L, E in alphabetical order.Apply quicksort to sort the list 7 2 9 10 5 4 4.2.8 Design an algorithm to rearrange elements of a given array of n real numbers so that all its negative elements proceed all its positive elements. Your algorithm should be both time- and space-efficient. Design and Analysis of Algorithms Chapter 432