请输入您要查询的字词:

 

单词 Quicksort
释义

quicksort


QuicksortMathworldPlanetmath is a divide-and-conquer algorithmMathworldPlanetmath for sorting in thecomparison model. Its expected running time is O(nlgn) forsorting n values.

Algorithm

Quicksort can be implemented recursively, as follows:

Algorithm Quicksort(L)
Input: A list L of n elements
Output: The list L in sorted order
if n>1 then
         p random element of L

A{x|xL,x<p}

B{z|zL,z=p}

C{y|yL,y>p}

SAQuicksort(A)

SCQuicksort(C)

return Concatenate(SA,B,SC)
else
         return L

Analysis

The behavior of quicksort can be analyzed by considering thecomputation as a binary treeMathworldPlanetmath. Each node of the tree corresponds toone recursive call to the quicksort procedure.

Consider the initial input to the algorithm, some list L. Call theSorted list S, with ith and jth elements Si and Sj. Thesetwo elements will be compared with some probability pij. Thisprobability can be determined by considering two preconditions onSi and Sj being compared:

  • Si or Sj must be chosen as a pivot p, since comparisonsonly occur against the pivot.

  • No element between Si and Sj can have already been chosenas a pivot before Si or Sj is chosen. Otherwise, would beseparated into different sublists in the recursion.

The probability of any particular element being chosen as the pivot isuniform. Therefore, the chance that Si or Sj is chosen as thepivot before any element between them is 2/(j-i+1). This isprecisely pij.

The expected number of comparisons is just the summation over allpossible comparisons of the probability of that particular comparisonoccurring. By linearity of expectation, no independence assumptionsare necessary. The expected number of comparisons is therefore

i=1nj>inpij=i=1nj>in2j-i+1(1)
=i=1nk=2n-i+12k(2)
2i=1nk=1n1k(3)
=2nHn=O(nlgn),(4)

where Hn is the nth Harmonic number.

The worst case behavior is Θ(n2), but this almost never occurs (with high probability it does not occur) with random pivots.

随便看

 

数学辞典收录了18232条数学词条,基本涵盖了常用数学知识及数学英语单词词组的翻译及用法,是数学学习的有利工具。

 

Copyright © 2000-2023 Newdu.com.com All Rights Reserved
更新时间:2025/5/4 6:10:10