SlideServe
Menu
Préférences
© SlideServe · v2026

Divide and Conquer

lamkin avatar
10 diapositives · 275 Views · Jan 07, 2020

Learn about the Quicksort algorithm, which efficiently reduces problem complexity by dividing and conquering the data using pivot selection and partitioning. Follow the steps to implement the algorithm for optimized sorting of lists.

Divide and Conquer - slide 1
01 / 10Divide and Conquer
1 / 17

Télécharger la présentation

Divide and Conquer
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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?