Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Divide and Conquer Reduce the problem by reducing the data set. The.

Similar presentations


Presentation on theme: "Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Divide and Conquer Reduce the problem by reducing the data set. The."— Presentation transcript:

1 Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Divide and Conquer Reduce the problem by reducing the data set. The argument being that a smaller data will easier to solve. Recursion

2 Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Quicksort outline  First choose some key from the list for which about half the keys will come before and half after. Call this key the pivot.  Then partition the items so that all those with keys less than the pivot come in one sublist, and all those with greater keys come in another.  Then sort the two reduced lists separately, put the sublists together, and the whole list will be in order.

3 Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Algorithm void QSort( int * A, int left, int right ) { int pivotIndex = Partition(A, left, right); if( pivotIndex > left + 1 ) QSort( A, left, pivotIndex ); if( pivotIndex < right - 2 ) QSort( A, pivotIndex + 1, right ); }

4 Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison 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

5 Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison 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?

6 Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Moving the elements  Work 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 "meet".

7 Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Searching 2040 1080605073010 0 9070 Pivot leftIndexrightIndex 2040 1030605078010 0 9070 [7][3] When indexes cross each, we stop.

8 Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Cross over  Low becomes new pivot  exchange old pivot with new pivot 2040 1030750608010 0 9070 Pivot leftIndexrightIndex [4][5] 207 10304050608010 0 9070

9 int Partition(int * A, int left, int right ) { intpivot = A[left]; intindexLeft= left+1; intindexRight= right - 1; while( indexLeft < indexRight ) { while (indexLeft < right && A[indexLeft] <= pivot) indexLeft++; while (A[indexRight] > pivot) indexRight--; if (indexLeft < indexRight) Swap (A, indexLeft, indexRight); } Swap(A, left, indexRight); //swap pivot & right index return indexRight; // new location of pivot }

10 Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Analysis " Recursively, the list is divided, resulting in O(log n) Original list What about the partition function?


Download ppt "Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT 143 - Gerard Harrison Divide and Conquer Reduce the problem by reducing the data set. The."

Similar presentations


Ads by Google