1 / 10

Divide and Conquer

Divide and Conquer. Reduce the problem by reducing the data set. The argument being that a smaller data will easier to solve. Recursion. 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 .

balin
Download Presentation

Divide and Conquer

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. 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 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. 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. 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

  5. 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. 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. Searching Pivot 40 20 10 80 60 50 7 30 100 90 70 When indexes cross each, we stop. leftIndex rightIndex [3] [7] 40 20 10 30 60 50 7 80 100 90 70

  8. Cross over • Low becomes new pivot • exchange old pivot with new pivot Pivot 40 20 10 30 7 50 60 80 100 90 70 rightIndex leftIndex [4] [5] 7 20 10 30 40 50 60 80 100 90 70

  9. int Partition(int * A, int left, int right ) { int pivot = A[left]; int indexLeft = left+1; int indexRight = 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. Analysis • Recursively, the list is divided, resulting in O(log n) Original list What about the partition function?

More Related