`
leonzhx
  • 浏览: 767707 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

QuickSort Analysis

阅读更多

1. Average Running Time of QuickSort:

  For every input array of length n , the average running time of QuickSort (with random pivots) is O(nlogn).

  Note: It holds for every input, no assumption on the input data. "Average" is over random pivots choice made by the algorithm.

 

2. For a certain input array A of length n.

    Sample Space Ω = All possible outcomes of random pivots choices in Quick Sort. ( A sample is a pivot sequence)

 

3. Random variable C defined on Ω :

  For any s in Ω , C(s) = number of comparisons between two input elements made by QuickSort (give random pivots choice s )

  While running time of QuickSort is dominated by comparisons, So we expected that E[C] = O(nlogn) (E[C] means the expectation of random variable C)

 

4. Zi = ith smallest element of A.

   For s in Ω, indices i < j , let Xij (s) = the number of times Zi, Zj get compared in QuickSort with pivot sequence s.

   Two elements get compared only when one is the pivot, which is excluded from future recursive calls. So Xij is an "Indicator" random variable. (the value is either 0 or 1 )

 

5. For s in Ω , C(s) = Sum(i=1 ~ n-1) {  Sum (j=i+1~n) {Xij(s) } } .

    By linearity of expectation : E[C] = Sum(i=1 ~ n-1) {  Sum (j=i+1~n) {E[Xij] } }

                                                       = Sum(i=1 ~ n-1) {  Sum (j=i+1~n) {P[Zi , Zj get compared] } }

 

6. A General Decomposition Principle

    a)  Identify random variable Y that you really care about.

    b)  Express Y as sum of indicator random variables :

                     Y = Sum (i =1 ~ n) {Xi}

    c)  Apply linearity of expectation :  E[Y] = Sum (i =1 ~ n) {P[Xi = 1] }

 

7. For a specific Zi , Zj with i < j , Consider Zi, Zi+1, Zi+2 ..., Zj-1, Zj.

    As long as none of these are chosen as a pivot, all are passed to the same recursive call.

    a)  if Zi, or Zj is the first one of these that gets chosen as pivot , then Zi and Zj get compared.

    b)  if one of Zi+1, ..., Zj-1 is the first one of these that gets chosen as pivot, then Zi and Zj are splitted into differenct recursive calls and are never compared.

    Since pivots are always chosen uniformly at random, each of Zi, Zi+1, ..., Zj-1, Zj is equally likely to be the first pivot. So P[Zi, Zj get compared] = 2/ (j - i + 1)

    So : E[C] = Sum(i=1 ~ n-1) {  Sum (j=i+1~n) {2/ (j - i + 1) } } = 2Sum(i=1 ~ n-1) {  Sum (j=i+1~n) {1/ (j - i + 1) } }

    For any fixed i , the inner sum is : Sum (j=i+1~n) {1/ (j - i + 1) } = 1/2 + 1/3 + ...

    And outer sum is at most n choices of i, so E[C] <= 2 n Sum ( k =2~n) {1/k} <= 2 n ln n

 

分享到:
评论

相关推荐

    PROBABILITY_QUICKSORT_ANALYSIS

    PROBABILITY_QUICKSORT_ANALYSIS

    MIT 麻省理工 算法课程-第九节-讲义(经典!)

    Relation of BSTs to Quicksort - Analysis of Random BST

    《算法导论》原版英文课件9

    Lecture 9 Relation of BSTs to Quicksort - Analysis of Random BST.pdf

    算法导论_英文第三版

    7.4 Analysis of quicksort 180 8 Sorting in Linear Time 191 8.1 Lower bounds for sorting 191 8.2 Counting sort 194 8.3 Radix sort 197 8.4 Bucket sort 200 9 Medians and Order Statistics 213 9.1 Minimum ...

    Introduction to Algorithms, 3rd edtion

    7.4 Analysis of quicksort 180 8 Sorting in Linear Time 191 8.1 Lower bounds for sorting 191 8.2 Counting sort 194 8.3 Radix sort 197 8.4 Bucket sort 200 9 Medians and Order Statistics 213 9.1 Minimum ...

    算法导论 第三版 英文原版 高清文字版

    7.4 Analysis of quicksort 180 8 Sorting in Linear Time 191 8.1 Lower bounds for sorting 191 8.2 Counting sort 194 8.3 Radix sort 197 8.4 Bucket sort 200 9 Medians and Order Statistics 213 9.1 Minimum ...

    算法导论-introduction to algrithms 3rd edition

    7.3 A randomized version of quicksort 179 7.4 Analysis of quicksort 180 8 Sorting in Linear Time 191 8.1 Lower bounds for sorting 191 8.2 Counting sort 194 8.3 Radix sort 197 8.4 Bucket sort 200 9 ...

    麻省理工算法导论(完整精辟版)

    Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures Chapter 11 - Hash Tables...

    【麻省理工大学】算法导论

    Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures Chapter 11 - Hash Tables...

    [麻省理工学院-算法导论](英文版).chm

    Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures Chapter 11 - Hash Tables...

    算法导论(英文第2版)

    Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures Chapter 11 - Hash Tables...

    [麻省理工学院-算法导论].Introduction.to. Algorithms,.Second.Edition

    Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 - Medians and Order Statistics Part III - Data Structures Chapter 10 - Elementary Data Structures Chapter 11 - Hash Tables...

    算法导论--Introduction.to.Algorithms

    7.4 Analysis of quicksort 180 8 Sorting in Linear Time 191 8.1 Lower bounds for sorting 191 8.2 Counting sort 194 8.3 Radix sort 197 8.4 Bucket sort 200 9 Medians and Order Statistics 213 9.1 Minimum ...

    算法导论教师手册基本上需要的题目都有

    Chapter 5: Probabilistic Analysis and Randomized Algorithms Lecture Notes 5-1 Solutions 5-8 Chapter 6: Heapsort Lecture Notes 6-1 Solutions 6-10 Chapter 7: Quicksort Lecture Notes 7-1 Solutions 7-9 ...

    Introsort-Analysis

    Musser发明,是一种混合排序算法,它运行Quicksort直到递归quicksort调用的数量达到2log 2 n。此时,Introsort切换到堆排序。当Introsort处理大小小于特定阈值的子阵列时,它将进一步切换到插入排序。 David R. ...

    算法导论英文版

    5.4 Probabi1istic analysis and further uses of indicator 106 II Sorting and Order Statistics Introduction 123 6 Heapsort 127 6.l Heaps I27 6.2 Maintaining the heap property 130 6.3 Building a heap 132...

    算法导论(中文 第二版)答案

     第七章 快速排序(Quicksort)  第八章 线性时间中的排序(Sorting in Linear Time)  第九章 中值与顺序统计(Medians and Order Statistics)  第三部分(Part III) 数据结构(Data Structures)  第十...

    算法导论 答案 (英文版)

    Chapter 5: Probabilistic Analysis and Randomized Algorithms Lecture Notes 5-1 Solutions 5-8 Chapter 6: Heapsort Lecture Notes 6-1 Solutions 6-10 Chapter 7: Quicksort Lecture Notes 7-1 Solutions 7-9 ...

Global site tag (gtag.js) - Google Analytics