Download presentation
Presentation is loading. Please wait.
Published byRodger Powell Modified over 9 years ago
1
QuickSort The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz
2
Divide and Conquer Reduce the problem by reducing the data set. A smaller data will easier to solve. Ideally, subdivide the data set into two equal parts Solve each part recursively
3
Divide and Conquer Very similar to MergeSort DIFFERENCE: MergeSort sorts things ‘on the way back up’, while QuickSort puts things into semi- sorted order on the way down So once we reach the base case(s), we’re done – we don’t need to merge stuff
4
QuickSort outline First choose some key from the array. This key the pivot. Ideally, about half the keys will come before and half after. Then partition the items so that all those with items less than the pivot are at the front of the array, and all those with greater values are at the back of the array. Put the pivot in between them Then sort the two reduced lists separately, and the whole list will be in order.
5
Algorithm Public void QSort( int [] A ) { QSort_private( A, 0, A.length – 1 ); } Private void QSort_private( int [] A, int left, int right ) { int pivotIndex = Partition(A, left, right); if( pivotIndex - 1 > left ) QSort( A, left, pivotIndex - 1); if( pivotIndex +1 < right ) QSort( A, pivotIndex + 1, right ); }
6
Partition Partition determines the pivot. Everything before pivot is less than. Everything after pivot is greater than. Pivot... These elements are all less than or equal to the pivot These elements are all greater than the pivot
7
Choosing the pivot Algorithm will work for any value we choose for pivot. Choose the first element (arbitrarily) as the pivot. Move all values less than or equal to pivot towards the beginning of array. Move all values greater towards the end. Where is the dividing line?
8
Moving the elements Work indeces inwards from both ends of the array. Start from "left" and look for first element greater than pivot. Start from "right" and look for first element less than pivot. Swap the two items. They will now be in the correct ends of the array. Repeat until searching indeces "meet".
9
Searching 2040 10806050730 Pivot leftIndexrightIndex 2040 10306050780100 90 [7][3] When indexes cross each other, we stop. 70 100 9070
10
Indeces “Meet” (Cross Over) Low becomes new pivot Exchange old pivot with new pivot 2040 10307506080 Pivot leftIndexrightIndex [4][5] 207 103040506080 100 9070 100 9070
11
int Partition(int[] A, int left, int right ) { intpivot = A[left]; intindexLeft= left; intindexRight= right; while( indexLeft < indexRight ) { while (A[indexRight] > pivot) indexRight--; while(indexLeft < indexRight && A[indexLeft]<=pivot) indexLeft++; if (indexLeft < indexRight) Swap (A, indexLeft, indexRight); } Swap(A, left, indexRight); //swap pivot & right index return indexRight; // new location of pivot }
12
Analysis: Average/Expected Case The list is divided in half each time, Resulting in O(log 2 n) Original list N elements N/2 N/4 } Log2(N) levels deep
13
Analysis: Average/Expected Case So how much time at each level? Partition will take O(N) time on each array (N = # elements) Partition will be run on all the sub-arrays of each level Therefore, on each level, all the calls to partition will take a total of O(N) time N = size of given array Original list N elements N/2 N/4 } Log2(N) levels deep
14
Analysis: Average/Expected Case Therefore, the expected time is O(N log 2 (N) ) In the average case What is the worst case? Original list N elements N/2 N/4 } Log2(N) levels deep
15
Analysis: Space O(log 2 (N) ) In the average case What is the worst case? Original list N elements N/2 N/4 } Log2(N) levels deep
16
Analysis: Worst Case Worst possible situation: we call partition, and split off a SINGLE element, leaving N-1 to be recursively sorted Original list N elements 1 N-1 1N-2 } N levels deep
17
Analysis: Worst Case Therefore, the expected time is O(N N ) = O(N 2 ) In the WORST case
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.