QuickSort - -To form L E we sort at both ends Complexity of...

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

View Full Document Right Arrow Icon
Quick Sort -A popular algorithm that also used “divde/conquer” -an “in place” algorithm that no additional memory is necessary to store temporary results QuickSort(s) → input: unsorted array s → output: sorted array s 1. Divide if s has only one element, return; otherwise -choose randomly an element “x” in s called the “pivot” -Divide s into three sets: L, E, G L → all elements < x E → all elements == x G → all elements > x 2. Resurse quicksort(L); quicksort(G); //At this point, L and G are sorted 3. Conquer put elements of L, E, and G back into s In-Place Quick Sort -We use the same array s to store L, E and G at each step -We use sub-ranges to represent each set -Last element in array is used as pivot
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: -To form L, E we sort at both ends Complexity of Quicksort-in the expected case at every call of quicksort, the pivot x will divide L and G in two sets that will, on average, be the same size-The work done in the algorithm for one element is the number of times the element is moved-in every call to qsSubrange we move each element only once in the subrange. Therefore an element is moved at most log(n) times-However in the worst case we could choose x (the pivot) such that either L or G are empty-Will only happen when the array is already sorted-Even with this problem, quicksort is very popular because it's fast and the common case is O(n log(n))...
View Full Document

This note was uploaded on 02/04/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

Ask a homework question - tutors are online