SlideShare a Scribd company logo
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)

More Related Content

What's hot (20)

PPTX
Quick sort
Dhruv Sabalpara
 
PPT
Quick Sort
priyankanaidu6
 
PPTX
Doubly Linked List
Ninad Mankar
 
PPTX
Insertion sort
almaqboli
 
PPT
Heap sort
Mohd Arif
 
PPTX
Presentation on the topic selection sort
District Administration
 
PPTX
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
PPTX
Merge sort algorithm
srutisenpatra
 
PPT
Merge sort
Vidushi Pathak
 
PDF
Quick sort
Abdelrahman Saleh
 
PPTX
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
PPTX
Divide and conquer - Quick sort
Madhu Bala
 
PPT
Sorting algorithms
CHANDAN KUMAR
 
PPTX
Selection sort
smlagustin
 
PPTX
Presentation-Merge Sort
Md Showrov Ahmed
 
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
PPTX
Demonstrate interpolation search
manojmanoj218596
 
PPT
Shell sorting
TUC
 
PDF
Sorting Algorithms
Mohammed Hussein
 
Quick sort
Dhruv Sabalpara
 
Quick Sort
priyankanaidu6
 
Doubly Linked List
Ninad Mankar
 
Insertion sort
almaqboli
 
Heap sort
Mohd Arif
 
Presentation on the topic selection sort
District Administration
 
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Merge sort algorithm
srutisenpatra
 
Merge sort
Vidushi Pathak
 
Quick sort
Abdelrahman Saleh
 
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
Divide and conquer - Quick sort
Madhu Bala
 
Sorting algorithms
CHANDAN KUMAR
 
Selection sort
smlagustin
 
Presentation-Merge Sort
Md Showrov Ahmed
 
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
Demonstrate interpolation search
manojmanoj218596
 
Shell sorting
TUC
 
Sorting Algorithms
Mohammed Hussein
 

Viewers also liked (7)

PPTX
Insertion and merge sort
Preetham Devisetty
 
PPT
3.8 quicksort
Krish_ver2
 
PPT
Safe laparoscopy
Mamdouh Sabry
 
PDF
Insertion Sort Algorithm
Gail Carmichael
 
PDF
Quicksort: illustrated step-by-step walk through
Yoshi Watanabe
 
PPT
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
Insertion and merge sort
Preetham Devisetty
 
3.8 quicksort
Krish_ver2
 
Safe laparoscopy
Mamdouh Sabry
 
Insertion Sort Algorithm
Gail Carmichael
 
Quicksort: illustrated step-by-step walk through
Yoshi Watanabe
 
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
Ad

Similar to Algorithm: Quick-Sort (20)

PPTX
quick sort by deepak.pptx
DeepakM509554
 
PPTX
Quick sort
AreenGaur
 
PPTX
CSE680-07QuickSort.pptx
DeepakM509554
 
PDF
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
PDF
Quicksort
Vasileios Lampos
 
PPT
s4_quick_sort.ppt
AliAhmad38278
 
PDF
Quicksort analysis
Premjeet Roy
 
PPTX
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
PDF
quick_sort
Mohamed Elsayed
 
PPTX
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
 
PPTX
Quick sort.pptx
Anbarasan Radhakrishnan R
 
PPTX
jyothi(22D21A0547)DAA.pptx in DAA computer
MadhurimaKomanduru
 
PPT
quicksort (1).ppt
Balasubramanian699229
 
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
PPT
MergesortQuickSort.ppt
AliAhmad38278
 
PPT
Quick Sort
Soumen Santra
 
PPT
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
PPTX
Quick sort by Sania Nisar
Sania Nisar
 
PDF
Skiena algorithm 2007 lecture08 quicksort
zukun
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
quick sort by deepak.pptx
DeepakM509554
 
Quick sort
AreenGaur
 
CSE680-07QuickSort.pptx
DeepakM509554
 
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Quicksort
Vasileios Lampos
 
s4_quick_sort.ppt
AliAhmad38278
 
Quicksort analysis
Premjeet Roy
 
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
quick_sort
Mohamed Elsayed
 
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
 
Quick sort.pptx
Anbarasan Radhakrishnan R
 
jyothi(22D21A0547)DAA.pptx in DAA computer
MadhurimaKomanduru
 
quicksort (1).ppt
Balasubramanian699229
 
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
MergesortQuickSort.ppt
AliAhmad38278
 
Quick Sort
Soumen Santra
 
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
Quick sort by Sania Nisar
Sania Nisar
 
Skiena algorithm 2007 lecture08 quicksort
zukun
 
Merge sort and quick sort
Shakila Mahjabin
 
Ad

More from Tareq Hasan (20)

PPTX
Grow Your Career with WordPress
Tareq Hasan
 
PDF
Caching in WordPress
Tareq Hasan
 
PDF
How to Submit a plugin to WordPress.org Repository
Tareq Hasan
 
PDF
Composer - The missing package manager for PHP
Tareq Hasan
 
PDF
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
Tareq Hasan
 
PPT
08 c++ Operator Overloading.ppt
Tareq Hasan
 
PPT
02 c++ Array Pointer
Tareq Hasan
 
PPT
01 c++ Intro.ppt
Tareq Hasan
 
PPT
chapter22.ppt
Tareq Hasan
 
PPT
chapter - 6.ppt
Tareq Hasan
 
PPT
Algorithm.ppt
Tareq Hasan
 
PPT
chapter-8.ppt
Tareq Hasan
 
PPT
chapter23.ppt
Tareq Hasan
 
PPT
chapter24.ppt
Tareq Hasan
 
PPT
Algorithm: priority queue
Tareq Hasan
 
PPT
Java: GUI
Tareq Hasan
 
PPT
Java: Inheritance
Tareq Hasan
 
PPT
Java: Exception
Tareq Hasan
 
PPT
Java: Introduction to Arrays
Tareq Hasan
 
PPT
Java: Class Design Examples
Tareq Hasan
 
Grow Your Career with WordPress
Tareq Hasan
 
Caching in WordPress
Tareq Hasan
 
How to Submit a plugin to WordPress.org Repository
Tareq Hasan
 
Composer - The missing package manager for PHP
Tareq Hasan
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
Tareq Hasan
 
08 c++ Operator Overloading.ppt
Tareq Hasan
 
02 c++ Array Pointer
Tareq Hasan
 
01 c++ Intro.ppt
Tareq Hasan
 
chapter22.ppt
Tareq Hasan
 
chapter - 6.ppt
Tareq Hasan
 
Algorithm.ppt
Tareq Hasan
 
chapter-8.ppt
Tareq Hasan
 
chapter23.ppt
Tareq Hasan
 
chapter24.ppt
Tareq Hasan
 
Algorithm: priority queue
Tareq Hasan
 
Java: GUI
Tareq Hasan
 
Java: Inheritance
Tareq Hasan
 
Java: Exception
Tareq Hasan
 
Java: Introduction to Arrays
Tareq Hasan
 
Java: Class Design Examples
Tareq Hasan
 

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)