SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Quick Sort Divide: Partition the array into two sub-arrays A[p . . q-1]  and A[q+1 . . r] such that each element of  A[p . . q-1] is less than or equal to A[q], which in turn less than or equal to each element of A[q+1 . . r]
Quick Sort Conquer: Sort the two sub-arrays A[p . . q-1]  and A[q+1 . . r] by recursive calls to quick sort.
Quick Sort Combine: Since the sub-arrays are sorted in place, no work is needed to combine them.
Quick Sort QUICKSORT(A, p, r) if p< r  then q    PARTITION(A, p, r) QUICKSORT(A, p, q-1) QUICKSORT(A, q+1, r)
Quick Sort PARTITION(A, p, r) x    A[r] i    p-1
Quick Sort for j    p to r-1 do if A[j] <= x then i   i+1 exchange A[i]       A[j] exchange A[i+1]       A[r] return i+1
Quick Sort (a) i 4 6 5 3 1 7 8 2 p, j r
Quick Sort (b) 4 6 5 3 1 7 8 2 j p, i r
Quick Sort (c) 4 6 5 3 1 7 8 2 j p, i r
Quick Sort (d) 4 6 5 3 1 7 8 2 j p, i r
Quick Sort (e) 4 6 5 3 8 7 1 2 j i p r
Quick Sort (f) 4 6 5 7 8 3 1 2 i p r j
Quick Sort (g) 4 6 5 7 8 3 1 2 i p r j
Quick Sort (h) 4 6 5 7 8 3 1 2 i p r
Quick Sort (i) 8 6 5 7 4 3 1 2 i p r
Quick Sort Worst-case partitioning: The partitioning routine produces one sub-problem with n-1 elements and another sub-problem with 0 elements. So the partitioning costs  θ (n) time.
Quick Sort Worst-case partitioning: The recurrence for the running time T(n)= T(n-1) + T(0) +  θ (n) =T(n-1) +  θ (n) =-----------------  θ (n 2 )
Quick Sort Worst-case partitioning: The  θ (n 2 ) running time occurs when the input array is already completely sorted – a common situation in which insertion sort runs in  O (n) time
Quick Sort Best-case partitioning: The partitioning procedure produces two sub-problems, each of size not more than n/2.
Quick Sort Best-case partitioning: The recurrence for the running time T(n) <= 2T(n/2) +  θ (n) = -----  O (n lg n)
Quick Sort Best-case partitioning: The equal balancing of the two sides of the partition at every level of the recursion produces faster algorithm.
Quick Sort Balanced partitioning: Suppose, the partitioning algorithm always produces 9-to-1 proportional split, which seems quite unbalanced.
Quick Sort Balanced partitioning: The recurrence for the running time T(n) <= T(9n/10) + T(n/10) +cn  = ------------ O (n lg n)
Quick Sort Balanced partitioning: The recursion tree
Quick Sort Balanced partitioning: In fact,  a 99-to-1 split yields an  O (n lg n) running time. Any split of constant proportionality yields a recursion tree of depth  θ (lg n)
Quick Sort Intuition for the average case: It is unlikely that the partitioning always happens in the same way at every level.
Quick Sort Intuition for the average case: In the average case, PARTION produces a mix of “good” and “bad” splits.
Quick Sort Intuition for the average case: The combination of the bad split followed by the good split produces three arrays of sizes 0,  (n-1)/2-1, and (n-1)/2 at a combined partitioning cost of  θ (n) +  θ (n-1)=  θ (n)  n n-1 (n-1)/2-1 (n-1)/2 0 Θ (n)
Quick Sort Intuition for the average case: A single level of partitioning produces two sub-arrays of  size (n-1)/2 at a cost of  θ (n).  n (n-1)/2 (n-1)/2 Θ (n)
A Randomized Version of Quick Sort Instead of always using A[r] as the pivot, we will use a randomly chosen element from the sub-array A[p..r].
A Randomized Version of Quick Sort Because the pivot element is randomly chosen, we expect the split of the input array to be reasonably well balanced on average.
A Randomized Version of Quick Sort RANDOMIZED-PARTITION(A, p, r) i    RANDOM(p, r) exchange A[r]       A[i] return PARTITION(A, p, r)
A Randomized Version of Quick Sort RANDOMIZED-QUICKSORT(A, p, r) if p<r then q    RANDOMIZED-PARTITION(A, p, r) RANDOMIZED-QUICKSORT(A, p, q-1) RANDOMIZED-QUICKSORT(A, q+1, r)

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Quick sort
Quick sortQuick sort
Quick sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Quick sort algorithm using slide presentation , Learn selection sort example ...
Quick sort algorithm using slide presentation , Learn selection sort example ...Quick sort algorithm using slide presentation , Learn selection sort example ...
Quick sort algorithm using slide presentation , Learn selection sort example ...
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Heap sort
Heap sortHeap sort
Heap sort
 
Time complexity
Time complexityTime complexity
Time complexity
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 

Andere mochten auch

Andere mochten auch (7)

Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 
Safe laparoscopy
Safe laparoscopySafe laparoscopy
Safe laparoscopy
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Quicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughQuicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk through
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 

Ähnlich wie Algorithm: Quick-Sort

Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2Deepak John
 
Introduction
IntroductionIntroduction
Introductionpilavare
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and AnalysisReetesh Gupta
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 

Ähnlich wie Algorithm: Quick-Sort (20)

Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Introduction
IntroductionIntroduction
Introduction
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
03 dc
03 dc03 dc
03 dc
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Merge sort
Merge sortMerge sort
Merge sort
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Lec10
Lec10Lec10
Lec10
 

Mehr von Tareq Hasan

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPressTareq Hasan
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPressTareq Hasan
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryTareq Hasan
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHPTareq Hasan
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011Tareq Hasan
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array PointerTareq Hasan
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.pptTareq Hasan
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queueTareq Hasan
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design ExamplesTareq Hasan
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 

Mehr von Tareq Hasan (20)

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPress
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org Repository
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHP
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
chapter22.ppt
chapter22.pptchapter22.ppt
chapter22.ppt
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
chapter-8.ppt
chapter-8.pptchapter-8.ppt
chapter-8.ppt
 
chapter23.ppt
chapter23.pptchapter23.ppt
chapter23.ppt
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design Examples
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 

Algorithm: Quick-Sort

  • 1. Quick Sort Divide: Partition the array into two sub-arrays A[p . . q-1] and A[q+1 . . r] such that each element of A[p . . q-1] is less than or equal to A[q], which in turn less than or equal to each element of A[q+1 . . r]
  • 2. Quick Sort Conquer: Sort the two sub-arrays A[p . . q-1] and A[q+1 . . r] by recursive calls to quick sort.
  • 3. Quick Sort Combine: Since the sub-arrays are sorted in place, no work is needed to combine them.
  • 4. Quick Sort QUICKSORT(A, p, r) if p< r then q  PARTITION(A, p, r) QUICKSORT(A, p, q-1) QUICKSORT(A, q+1, r)
  • 5. Quick Sort PARTITION(A, p, r) x  A[r] i  p-1
  • 6. Quick Sort for j  p to r-1 do if A[j] <= x then i  i+1 exchange A[i]   A[j] exchange A[i+1]   A[r] return i+1
  • 7. Quick Sort (a) i 4 6 5 3 1 7 8 2 p, j r
  • 8. Quick Sort (b) 4 6 5 3 1 7 8 2 j p, i r
  • 9. Quick Sort (c) 4 6 5 3 1 7 8 2 j p, i r
  • 10. Quick Sort (d) 4 6 5 3 1 7 8 2 j p, i r
  • 11. Quick Sort (e) 4 6 5 3 8 7 1 2 j i p r
  • 12. Quick Sort (f) 4 6 5 7 8 3 1 2 i p r j
  • 13. Quick Sort (g) 4 6 5 7 8 3 1 2 i p r j
  • 14. Quick Sort (h) 4 6 5 7 8 3 1 2 i p r
  • 15. Quick Sort (i) 8 6 5 7 4 3 1 2 i p r
  • 16. Quick Sort Worst-case partitioning: The partitioning routine produces one sub-problem with n-1 elements and another sub-problem with 0 elements. So the partitioning costs θ (n) time.
  • 17. Quick Sort Worst-case partitioning: The recurrence for the running time T(n)= T(n-1) + T(0) + θ (n) =T(n-1) + θ (n) =----------------- θ (n 2 )
  • 18. Quick Sort Worst-case partitioning: The θ (n 2 ) running time occurs when the input array is already completely sorted – a common situation in which insertion sort runs in O (n) time
  • 19. Quick Sort Best-case partitioning: The partitioning procedure produces two sub-problems, each of size not more than n/2.
  • 20. Quick Sort Best-case partitioning: The recurrence for the running time T(n) <= 2T(n/2) + θ (n) = ----- O (n lg n)
  • 21. Quick Sort Best-case partitioning: The equal balancing of the two sides of the partition at every level of the recursion produces faster algorithm.
  • 22. Quick Sort Balanced partitioning: Suppose, the partitioning algorithm always produces 9-to-1 proportional split, which seems quite unbalanced.
  • 23. Quick Sort Balanced partitioning: The recurrence for the running time T(n) <= T(9n/10) + T(n/10) +cn = ------------ O (n lg n)
  • 24. Quick Sort Balanced partitioning: The recursion tree
  • 25. Quick Sort Balanced partitioning: In fact, a 99-to-1 split yields an O (n lg n) running time. Any split of constant proportionality yields a recursion tree of depth θ (lg n)
  • 26. Quick Sort Intuition for the average case: It is unlikely that the partitioning always happens in the same way at every level.
  • 27. Quick Sort Intuition for the average case: In the average case, PARTION produces a mix of “good” and “bad” splits.
  • 28. Quick Sort Intuition for the average case: The combination of the bad split followed by the good split produces three arrays of sizes 0, (n-1)/2-1, and (n-1)/2 at a combined partitioning cost of θ (n) + θ (n-1)= θ (n) n n-1 (n-1)/2-1 (n-1)/2 0 Θ (n)
  • 29. Quick Sort Intuition for the average case: A single level of partitioning produces two sub-arrays of size (n-1)/2 at a cost of θ (n). n (n-1)/2 (n-1)/2 Θ (n)
  • 30. A Randomized Version of Quick Sort Instead of always using A[r] as the pivot, we will use a randomly chosen element from the sub-array A[p..r].
  • 31. A Randomized Version of Quick Sort Because the pivot element is randomly chosen, we expect the split of the input array to be reasonably well balanced on average.
  • 32. A Randomized Version of Quick Sort RANDOMIZED-PARTITION(A, p, r) i  RANDOM(p, r) exchange A[r]   A[i] return PARTITION(A, p, r)
  • 33. A Randomized Version of Quick Sort RANDOMIZED-QUICKSORT(A, p, r) if p<r then q  RANDOMIZED-PARTITION(A, p, r) RANDOMIZED-QUICKSORT(A, p, q-1) RANDOMIZED-QUICKSORT(A, q+1, r)