1 / 17

QuickSort

QuickSort. The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz. 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

peta
Download Presentation

QuickSort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 ) { intpivotIndex = Partition(A, left, right); if( pivotIndex - 1 > left ) QSort_private( A, left, pivotIndex - 1); if( pivotIndex +1 < right ) QSort_private( A, pivotIndex + 1, right ); }

  6. Partition • Partition determines the pivot. • Everything before pivot is less than. • Everything after pivot is greater than. These elements are all less than or equal to the pivot These elements are all greater than the pivot ... ... 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 Pivot 40 20 10 80 60 50 7 30 90 70 100 When indexes cross each other, we stop. leftIndex rightIndex [3] [7] 90 70 40 20 10 30 60 50 7 80 100

  10. Indeces “Meet” (Cross Over) • Low becomes new pivot • Exchange old pivot with new pivot Pivot 40 20 10 30 7 50 60 80 90 70 100 rightIndex leftIndex [4] [5] 90 70 100 7 20 10 30 40 50 60 80

  11. int Partition(int[] A, int left, int right ) { int pivot = 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(log2 n) } Original list Log2(N) levels deep N elements N/2 N/2 N/4 N/4 N/4 N/4

  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 Log2(N) levels deep N elements N/2 N/2 N/4 N/4 N/4 N/4

  14. Analysis: Average/Expected Case • Therefore, the expected time is O(N • log2(N) ) • In the average case • What is the worst case? } Original list Log2(N) levels deep N elements N/2 N/2 N/4 N/4 N/4 N/4

  15. Analysis: Space • O(log2(N) ) • In the average case • What is the worst case? } Original list Log2(N) levels deep N elements N/2 N/2 N/4 N/4 N/4 N/4

  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 levels deep N elements 1 N-1 1 N-2

  17. Analysis: Worst Case • Therefore, the expected time is O(N • N ) = O(N2) • In the WORST case space is O( N )

More Related