`
leonzhx
  • 浏览: 786031 次
  • 性别: 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")这个主题中,我们将深入探讨快速排序在最坏、最好和平均情况下的性能,并引入概率分析来理解其在大规模数据中的行为。 快速排序的主要步骤如下: 1....

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

    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...

    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...

    Algorithms_for_Data_Analysis

    "Algorithms_for_Data_Analysis" 这个主题深入探讨了如何利用各种算法来提升数据分析的效率和准确性。这里我们将围绕这个主题,详细讨论在数据分析中常见的算法及其应用场景。 1. **排序算法**:在数据预处理阶段,...

    Algorithms in C++, Parts 1-4: Fundamentals, Data Structures, Sorting, Searching

    An example is quicksort, where the partitioning step is a constant-time operation, but the recursive calls are made on two smaller subarrays. 6. **Examples of Algorithm Analysis**: Detailed examples...

    ECE345-algorithm-and-data-structure

    ECE345-算法和数据结构 这个存储库中的代码是我在多伦多大学的 ECE345 实验室和作业的解决方案。 请不要将其用于您自己的课程作业,谢谢。 奖金分配 ... • Sorting: quicksort and analysis, heapsor

    算法导论第三版 solution(英文版)

    Chapter 7: Quicksort Lecture Notes 7-1 Solutions 7-9 Chapter 8: Sorting in Linear Time Lecture Notes 8-1 Solutions 8-10 Chapter 9: Medians and Order Statistics Lecture Notes 9-1 Solutions 9-10 Chapter...

    算法导论-第三版(中文).rar

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

    算法导论(原书第二版)

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

    算法导论Introduction to Algorithms

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

Global site tag (gtag.js) - Google Analytics