Math for the people, by the people.
Encyclopedia | Requests | Forums | Docs | Wiki | Random | RSS
 quicksort (Definition)

Quicksort is a divide-and-conquer algorithm for sorting in the comparison model. Its expected running time is for sorting values.

## Algorithm

Quicksort can be implemented recursively, as follows:

Algorithm QUICKSORT()
Input: A list of elements
Output: The list in sorted order
if then

else

## Analysis

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

Consider the initial input to the algorithm, some list . Call the Sorted list with th and th elements and These two elements will be compared with some probability . This probability can be determined by considering two preconditions on and being compared:

• or must be chosen as a pivot , since comparisons only occur against the pivot.
• No element between and can have already been chosen as a pivot before or is chosen. Otherwise, would be separated into different sublists in the recursion.

The probability of any particular element being chosen as the pivot is uniform. Therefore, the chance that or is chosen as the pivot before any element between them is . This is precisely

The expected number of comparisons is just the summation over all possible comparisons of the probability of that particular comparison occurring. By linearity of expectation, no independence assumptions are necessary. The expected number of comparisons is therefore

 (1) (2) (3) (4)

where is the th Harmonic number.

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

"quicksort" is owned by thouis.
(view preamble)

 View style: HTML with imagespage imagesTeX source

 Keywords: comparison sort, quick-sort

Cross-references: harmonic number, necessary, expectation, summation, separated, pivot, recursive, tree, node, binary tree
There are 4 references to this entry.

This is version 10 of quicksort, born on 2003-10-07, modified 2006-08-06.
Object id is 4763, canonical name is Quicksort.
Accessed 7416 times total.

Classification:
 AMS MSC: 68P10 (Computer science :: Theory of data :: Searching and sorting)

 None.[ View all 3 ]
 Discussion

No messages.

 Interact rate | post | correct | update request | add derivation | add example | add (any)