QuickSort

# uses comparisons uses n comparisons and less

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ize n-1 and 1 Then it works on the larger subfile Total, Total, it requires: (n-1) + (n-2) + … + 1 = n*((n + 1) / 2 2) 1) = n2/2-n/2 comparisons /2! Result: Result: O(n2) ! Note: Note: max recursion depth is n = O(n) 20 Quicksort: Memory complexity Additional Additional memory is needed for the stack in function calls (and it is proportional to the the maximum recursion depth) ! Expect case: O(log n) Expect (log ! Worst case: O(n) Worst 21 Quicksort: improvements ! Selecting Selecting a good pivot by random selection selection of pivot, median of median, or search search for median ! Reduce Reduce overhead when subarray becomes small by employing other sorting algorithms ! Fewer recursions by using tail recursion 22 Quicksort: Pivot Selection Strategies ! ! ! ! ! Median as pivot (O(n) but not practical) Median-ofMedian-of-three: median of first, middle, last elements elements Median-of-three Median-of-three as the first pivot, and means of of subarrays as subsequent pivots (Meansort) Random position: O(n log n) with high probability log Middle Middle element (Bsort) – When incrementing and decrementing “down” and “up”, When perform perform comparisons and swaps of neighbors as in bidirectional directional bubble sort to find largest item of left subarray and least least item of right subarray – Detect sortedness – A subarray of size two is sorted – To sort a subarray of size three, only one comparison is needed 23 Quicksort: Quicksort: Median-of-three // find median of a, b, and c if (a < b) if if (b < c) median median = b; else if ( a < c) median = c; else median = a; else if (c < b) median = b; else if (c < a) median = c; else median = a; 24 Quicksort: Quicksort: small/sorted arrays ! Sort Sort small arrays (e.g., 2 or 3) by explicit comparisons comparisons and exc...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern