SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Quick Sort

 Characteristics
   sorts almost in "place," i.e., does not require
    an additional array
   very practical, average sort performance O(n
    log n) (with small constant factors), but worst
    case O(n2)




                                                      1
Quick Sort – the Principle

 To understand quick-sort, let’s look at a
  high-level description of the algorithm
 A divide-and-conquer algorithm
   Divide: partition array into 2 subarrays such
    that elements in the lower part <= elements in
    the higher part
   Conquer: recursively sort the 2 subarrays
   Combine: trivial since sorting is done in place

                                                  2
Partitioning
 Linear     time partitioning procedure
Partition(A,p,r)                i i                            j j
01   x A[r]                       17 12    6   19 23   8   5   10
02   i p-1
03   j r+1            X=10             i                   j
04   while TRUE
                                  10 12    6   19 23   8   5   17
05      repeat j j-1
06         until A[j] x
                                               i       j
07      repeat i i+1
08         until A[i] x           10   5   6   19 23   8   12 17
09      if i<j
10         then exchange A[i]   A[j]
                                               j   i
11         else return j
                                  10   5   6   8   23 19 12 17

                                                                3
Quick Sort Algorithm

 Initial   call Quicksort(A, 1, length[A])
    Quicksort(A,p,r)
    01 if p<r
    02    then q   Partition(A,p,r)
    03         Quicksort(A,p,q)
    04         Quicksort(A,q+1,r)




                                              4
Analysis of Quicksort

 Assume    that all input elements are distinct
 The running time depends on the
  distribution of splits




                                               5
Best Case
   If we are lucky, Partition splits the array evenly
              T (n) 2T (n / 2)     (n)




                                                         6
Worst Case
 What is the worst case?
 One side of the parition has only one element

   T (n) T (1) T (n 1)              ( n)
         T (n 1)             ( n)
          n
                      (k )
         k 1
                  n
              (         k)
                  k 1

              (n 2 )

                                             7
Worst Case (2)




                 8
Worst Case (3)
 When   does the worst case appear?
   input is sorted
   input reverse sorted

 Same   recurrence for the worst case of
  insertion sort
 However, sorted input yields the best case
  for insertion sort!


                                               9
Analysis of Quicksort
 Suppose   the split is 1/10 : 9/10
  T (n) T (n /10) T (9n /10)   (n)     (n log n)!




                                                    10
An Average Case Scenario
   Suppose, we alternate           L( n) 2U ( n / 2)      (n ) lucky
    lucky and unlucky               U ( n) L( n 1)     (n) unlucky
    cases to get an                 we consequently get
    average behavior                L( n) 2( L(n / 2 1)    (n / 2)) (n )
                                           2 L(n / 2 1)       (n )
          n                   (n)            (n log n )

                                                n             (n)
    1             n-1


        (n-1)/2         (n-1)/2     (n-1)/2+1           (n-1)/2
                                                                        11
An Average Case Scenario (2)
   How can we make sure that we are usually
    lucky?
     Partition around the ”middle” (n/2th) element?
     Partition around a random element (works well in
      practice)
   Randomized algorithm
     running time is independent of the input ordering
     no specific input triggers worst-case behavior
     the worst-case is only determined by the output of the
      random-number generator

                                                          12
Randomized Quicksort
 Assume    all elements are distinct
 Partition around a random element
 Consequently, all splits (1:n-1, 2:n-2, ..., n-
  1:1) are equally likely with probability 1/n

 Randomization   is a general tool to improve
  algorithms with bad worst-case but good
  average-case complexity

                                                13
Randomized Quicksort (2)
    Randomized-Partition(A,p,r)
    01 i   Random(p,r)
    02 exchange A[r]     A[i]
    03 return Partition(A,p,r)

    Randomized-Quicksort(A,p,r)
    01 if p < r then
    02    q     Randomized-Partition(A,p,r)
    03    Randomized-Quicksort(A,p,q)
    04    Randomized-Quicksort(A,q+1,r)



                                              14
Randomized Quicksort Analysis
 Let T(n) be the expected number of comparisons
  needed to quicksort n numbers.
 Since each split occurs with probability 1/n, T(n)
  has value T(i-1)+T(n-i)+n-1 with probability 1/n.
 Hence,




                                                  15
Randomized Quicksort Analysis(2)
 We    have seen this recurrence before.
 It is the recurrence for the expected
  number of comparisons required to insert
  a randomly chosen permutation of n
  elements.
 We proved that T(n) = O(nlog2 n).
 Hence expected number of comparisons
  required by randomized quicksort is
  O(nlog2 n)
                                             16
Randomized Quicksort running times


 Worst   case running time of quicksort is
  O(n2)
 Best case running time of quicksort is
  O(nlog2 n)
 Expected running time of quicksort is
  O(nlog2 n)


                                              17
What does expected running time mean?
 The running time of quicksort does not depend
  on the input. It depends on the random numbers
  provided by the generator.
 Thus for the same input the program might take
  3sec today and 5sec tomorrow.
 The average time taken over many different runs
  of the program would give us the expected time.
 Same as saying that we are taking average over
  all possible random number sequences provided
  by the generator.
                                                18
Analysis of insertion in BST
 When creating a binary search tree on n
  elements the running time does depend on the
  order of the elements.
 Our algorithm for insertion did not employ an
  random bits.
 Given a specific input order the algorithm takes
  the same time each day.
 However, the time taken is different for different
  input orders.
 The average time taken over all possible input
  orders is O(nlog2 n).
                                                       19

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Lec5
Lec5Lec5
Lec5
 
Lec20
Lec20Lec20
Lec20
 
Lec3
Lec3Lec3
Lec3
 
Lec11
Lec11Lec11
Lec11
 
Lec1
Lec1Lec1
Lec1
 
Lec3
Lec3Lec3
Lec3
 
Newton-Raphson Method
Newton-Raphson MethodNewton-Raphson Method
Newton-Raphson Method
 
Sommation séries entières
Sommation séries entièresSommation séries entières
Sommation séries entières
 
ABC with Wasserstein distances
ABC with Wasserstein distancesABC with Wasserstein distances
ABC with Wasserstein distances
 
Lec2
Lec2Lec2
Lec2
 
Ppt geometri analit ruang
Ppt geometri analit ruangPpt geometri analit ruang
Ppt geometri analit ruang
 
Met num 8
Met num 8Met num 8
Met num 8
 
Teori pappus
Teori pappusTeori pappus
Teori pappus
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
Oral 1 CAPES Maths - L'intégration en terminale
Oral 1 CAPES Maths - L'intégration en terminaleOral 1 CAPES Maths - L'intégration en terminale
Oral 1 CAPES Maths - L'intégration en terminale
 
03 truncation errors
03 truncation errors03 truncation errors
03 truncation errors
 
Solusi D'Alembert Pers. Gelombang 1D
Solusi D'Alembert Pers. Gelombang 1DSolusi D'Alembert Pers. Gelombang 1D
Solusi D'Alembert Pers. Gelombang 1D
 
solucionario de purcell 0
solucionario de purcell 0solucionario de purcell 0
solucionario de purcell 0
 
Interpolasi linier
Interpolasi linierInterpolasi linier
Interpolasi linier
 
Topology for Computing: Homology
Topology for Computing: HomologyTopology for Computing: Homology
Topology for Computing: Homology
 

Andere mochten auch

Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applicationsyazad dumasia
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 
Catherine durgin mdsk 6162
Catherine durgin mdsk 6162Catherine durgin mdsk 6162
Catherine durgin mdsk 6162Catherine Durgin
 
Noches griegas
Noches griegasNoches griegas
Noches griegasfilipj2000
 
Chapter 10 be wto_agriculture
Chapter 10 be wto_agricultureChapter 10 be wto_agriculture
Chapter 10 be wto_agricultureHo Cao Viet
 
Microsoft Power Point Sig Presentation Final
Microsoft Power Point   Sig Presentation FinalMicrosoft Power Point   Sig Presentation Final
Microsoft Power Point Sig Presentation Finalsimgesm
 
Observations made by the group
Observations made by the groupObservations made by the group
Observations made by the groupd3ath2u
 
Brand Management Pptsam1
Brand Management Pptsam1Brand Management Pptsam1
Brand Management Pptsam1samarpankole
 

Andere mochten auch (20)

Lec13
Lec13Lec13
Lec13
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Master theorem
Master theoremMaster theorem
Master theorem
 
Master method
Master methodMaster method
Master method
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Catherine durgin mdsk 6162
Catherine durgin mdsk 6162Catherine durgin mdsk 6162
Catherine durgin mdsk 6162
 
Noches griegas
Noches griegasNoches griegas
Noches griegas
 
The Voice 2000 edition
The Voice 2000 editionThe Voice 2000 edition
The Voice 2000 edition
 
Chapter 10 be wto_agriculture
Chapter 10 be wto_agricultureChapter 10 be wto_agriculture
Chapter 10 be wto_agriculture
 
Microsoft Power Point Sig Presentation Final
Microsoft Power Point   Sig Presentation FinalMicrosoft Power Point   Sig Presentation Final
Microsoft Power Point Sig Presentation Final
 
Project Management Fundamentals Course
Project Management Fundamentals CourseProject Management Fundamentals Course
Project Management Fundamentals Course
 
App storeとandroid market
App storeとandroid marketApp storeとandroid market
App storeとandroid market
 
Observations made by the group
Observations made by the groupObservations made by the group
Observations made by the group
 
Absolute monarchy
Absolute monarchyAbsolute monarchy
Absolute monarchy
 
Brand Management Pptsam1
Brand Management Pptsam1Brand Management Pptsam1
Brand Management Pptsam1
 
Canaima
CanaimaCanaima
Canaima
 
one
oneone
one
 

Ähnlich wie Lec10

Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Design and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and ConquerDesign and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and ConquerSeshu Chakravarthy
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
lecture 7
lecture 7lecture 7
lecture 7sajinsc
 
Top school in noida
Top school in noidaTop school in noida
Top school in noidaEdhole.com
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Daa divide-n-conquer
Daa divide-n-conquerDaa divide-n-conquer
Daa divide-n-conquersankara_rao
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with examplemaamir farooq
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptxpallavidhade2
 
Introduction - Time Series Analysis
Introduction - Time Series AnalysisIntroduction - Time Series Analysis
Introduction - Time Series Analysisjaya gobi
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 

Ähnlich wie Lec10 (20)

Lec10
Lec10Lec10
Lec10
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Design and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and ConquerDesign and Analysis of Algorithms - Divide and Conquer
Design and Analysis of Algorithms - Divide and Conquer
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
lecture 7
lecture 7lecture 7
lecture 7
 
Top school in noida
Top school in noidaTop school in noida
Top school in noida
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Daa divide-n-conquer
Daa divide-n-conquerDaa divide-n-conquer
Daa divide-n-conquer
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Mid term solution
Mid term solutionMid term solution
Mid term solution
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with example
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Introduction - Time Series Analysis
Introduction - Time Series AnalysisIntroduction - Time Series Analysis
Introduction - Time Series Analysis
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 

Lec10

  • 1. Quick Sort  Characteristics  sorts almost in "place," i.e., does not require an additional array  very practical, average sort performance O(n log n) (with small constant factors), but worst case O(n2) 1
  • 2. Quick Sort – the Principle  To understand quick-sort, let’s look at a high-level description of the algorithm  A divide-and-conquer algorithm  Divide: partition array into 2 subarrays such that elements in the lower part <= elements in the higher part  Conquer: recursively sort the 2 subarrays  Combine: trivial since sorting is done in place 2
  • 3. Partitioning  Linear time partitioning procedure Partition(A,p,r) i i j j 01 x A[r] 17 12 6 19 23 8 5 10 02 i p-1 03 j r+1 X=10 i j 04 while TRUE 10 12 6 19 23 8 5 17 05 repeat j j-1 06 until A[j] x i j 07 repeat i i+1 08 until A[i] x 10 5 6 19 23 8 12 17 09 if i<j 10 then exchange A[i] A[j] j i 11 else return j 10 5 6 8 23 19 12 17 3
  • 4. Quick Sort Algorithm  Initial call Quicksort(A, 1, length[A]) Quicksort(A,p,r) 01 if p<r 02 then q Partition(A,p,r) 03 Quicksort(A,p,q) 04 Quicksort(A,q+1,r) 4
  • 5. Analysis of Quicksort  Assume that all input elements are distinct  The running time depends on the distribution of splits 5
  • 6. Best Case  If we are lucky, Partition splits the array evenly T (n) 2T (n / 2) (n) 6
  • 7. Worst Case  What is the worst case?  One side of the parition has only one element T (n) T (1) T (n 1) ( n) T (n 1) ( n) n (k ) k 1 n ( k) k 1 (n 2 ) 7
  • 9. Worst Case (3)  When does the worst case appear?  input is sorted  input reverse sorted  Same recurrence for the worst case of insertion sort  However, sorted input yields the best case for insertion sort! 9
  • 10. Analysis of Quicksort  Suppose the split is 1/10 : 9/10 T (n) T (n /10) T (9n /10) (n) (n log n)! 10
  • 11. An Average Case Scenario  Suppose, we alternate L( n) 2U ( n / 2) (n ) lucky lucky and unlucky U ( n) L( n 1) (n) unlucky cases to get an we consequently get average behavior L( n) 2( L(n / 2 1) (n / 2)) (n ) 2 L(n / 2 1) (n ) n (n) (n log n ) n (n) 1 n-1 (n-1)/2 (n-1)/2 (n-1)/2+1 (n-1)/2 11
  • 12. An Average Case Scenario (2)  How can we make sure that we are usually lucky?  Partition around the ”middle” (n/2th) element?  Partition around a random element (works well in practice)  Randomized algorithm  running time is independent of the input ordering  no specific input triggers worst-case behavior  the worst-case is only determined by the output of the random-number generator 12
  • 13. Randomized Quicksort  Assume all elements are distinct  Partition around a random element  Consequently, all splits (1:n-1, 2:n-2, ..., n- 1:1) are equally likely with probability 1/n  Randomization is a general tool to improve algorithms with bad worst-case but good average-case complexity 13
  • 14. Randomized Quicksort (2) Randomized-Partition(A,p,r) 01 i Random(p,r) 02 exchange A[r] A[i] 03 return Partition(A,p,r) Randomized-Quicksort(A,p,r) 01 if p < r then 02 q Randomized-Partition(A,p,r) 03 Randomized-Quicksort(A,p,q) 04 Randomized-Quicksort(A,q+1,r) 14
  • 15. Randomized Quicksort Analysis  Let T(n) be the expected number of comparisons needed to quicksort n numbers.  Since each split occurs with probability 1/n, T(n) has value T(i-1)+T(n-i)+n-1 with probability 1/n.  Hence, 15
  • 16. Randomized Quicksort Analysis(2)  We have seen this recurrence before.  It is the recurrence for the expected number of comparisons required to insert a randomly chosen permutation of n elements.  We proved that T(n) = O(nlog2 n).  Hence expected number of comparisons required by randomized quicksort is O(nlog2 n) 16
  • 17. Randomized Quicksort running times  Worst case running time of quicksort is O(n2)  Best case running time of quicksort is O(nlog2 n)  Expected running time of quicksort is O(nlog2 n) 17
  • 18. What does expected running time mean?  The running time of quicksort does not depend on the input. It depends on the random numbers provided by the generator.  Thus for the same input the program might take 3sec today and 5sec tomorrow.  The average time taken over many different runs of the program would give us the expected time.  Same as saying that we are taking average over all possible random number sequences provided by the generator. 18
  • 19. Analysis of insertion in BST  When creating a binary search tree on n elements the running time does depend on the order of the elements.  Our algorithm for insertion did not employ an random bits.  Given a specific input order the algorithm takes the same time each day.  However, the time taken is different for different input orders.  The average time taken over all possible input orders is O(nlog2 n). 19