Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Quicksort

Old News

See Also Recommended Books Recommended Links Donald Knuth
Animations Test suits History Humor Etc

Quicksort is an efficient and until lately fashionable sorting algorithm invented by C.A.R. Hoare in 1962 and was first publish in Britich Computer Society monthly Computer Journal  (Hoare, C. A. R. "Quicksort." Computer Journal 5 (1): 10-15. (1962). ).

The quicksort algorithm requires Ω(log n) extra storage space, making it not a strictly in-place algorithm. It is important to understand that Quicksort can go quadratic in the worst case. The worst case time for Quicksort is the same as for insertion sort and other simple algorithms, which is substantially worse than Heapsort or Mergesort. And far from optimal for Quicksort cases are far more frequent in real data that people assume.  Moreover "bad data" for Quicksort can arise dynamically during sorting large non-random array. This "poisoning of data" behavior is an interesting phenomenon that requires further study.   See [PDF] A Killer Adversary for Quicksort

The algorithm is well described in Quicksort - Wikipedia, the free encyclopedia  so will will concentrate of description of weakness rather then simple tutorial.

Algorithms has two phases:

It is important to understand that quicksort is efficient only on the average, and its worst case is n2 , while heapsort has an interesting property that the worst case is not much different form the an average case.  So more modern textbooks actually pay more attention to heapsort then quicksort. moreover ability to mentioned and discuss weaknesses of quicksort can serve as a litmus text of the quality of the textbook.   As size of the array increases merge-sort became more and more competitive and eventually overtakes both.

In presudocode the algorithm looks like:

  1. pick one element in the array, which will be the pivot.
  2. make one pass through the array, called a partition step, re-arranging the entries so that:
  3. recursively apply quicksort to the part of the array that is to the left of the pivot, and to the part on its right until the size of the partition is less then predefined small number M. For example M=5. Them apply some simple sorting algorith to each partition.   If M=1 then sorting phase is not needed.
It is clear that the main work is done in the partition phase so the quality of division of the set of two subpartitions is crucial. For the strategy to be effective, the partition phase must ensure that two partitions are almost equal in size.  This is pretty complex problem that actually does not have a satisfactory solutions. The most common solution is "selection-based pivoting" In worst case one of the partitions is of size one and quicksort becomes very inefficient.  Also problematic are:

In general, Quicksort a good example of the divide and conquer strategy for solving problems (the code borrowed from Data Structures and Algorithms Quick Sort. This example presuppose M=1 so sorting phase is absent )

quicksort( void *a, int low, int high )
  {
  int pivot;
  /* Termination condition! */
  if ( high > low )
    {
    pivot = partition( a, low, high );
    quicksort( a, low, pivot-1 );
    quicksort( a, pivot+1, high );
    }
  }

Initial Step - First Partition

Sort Left Partition in the same way
 

As you can see we choose a pivot element and arrange that all the items in the lower part are less than the pivot and all those in the upper part greater than it. In the most general case, we don't know anything about the items to be sorted, so that any choice of the pivot element will do - the first element is a convenient one.

As an illustration of this idea, you can view this animation, which shows a partition algorithm in which items to be sorted are copied from the original array to a new one: items smaller than the pivot are placed to the left of the new array and items greater than the pivot are placed on the right. In the final step, the pivot is dropped into the remaining slot in the middle.

Talks:

Animations

Presentations


Notes:
  • This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Some amount of grammar and spelling errors should be expected.
  • The site contain some broken links as it develops like a living tree... Please try to use Google, Open directory, etc. to find a replacement link (see HOWTO search the WEB for details). We would appreciate if you can mail us a correct link.
Google Search
Open directory

Research Index


Old News ;-)

[Apr 10, 2009] Quicksort - C++, Java, Algorithm and Data Structure Tutorials by Denis Kulagin

Contains Java and C++ implementation...

Recommended books

  1. Cormen, Leiserson, Rivest. Introduction to algorithms. (Theory)
  2. Aho, Ullman, Hopcroft. Data Structures and Algorithms. (Theory)
  3. Robert Lafore. Data Structures and Algorithms in Java. (Practice)
  4. Mark Allen Weiss. Data Structures and Problem Solving Using C++. (Practice)

Visualizers

  1. Quicksort Animation (with source code line by line visualization)
  2. Quicksort in Java Applets Centre
  3. Animated Sorting Algorithms: Quicksort

ICS 161: Design and Analysis of Algorithms. Lecture notes for January 18, 1996

Three Divide and Conquer Sorting Algorithms
Today we'll finish heapsort, and describe both mergesort and quicksort. Why do we need multiple sorting algorithms? Different methods work better in different applications.

Recommended Links

Quicksort - Wikipedia, the free encyclopedia

Quicksort -- from Wolfram MathWorld

[PDF] QUICKSORT IS OPTIMAL Robert Sedgewick Jon  the quicksort cheerleader paper. Very questionable argumentation.  Optimal on what? On random data ?

NIST: balanced quicksort

Definition: A variant of quicksort which attempts to choose a pivot likely to represent the middle of the values to be sorted.

See also median.

Note: This is one of several attempts to prevent bad worst cases in quicksort by choosing the initial pivot.

Author: ASK

QuickSort Algorithm

Lecture 5 - quicksort

Analysis of Quicksort (6) Michael L. Littman; September 15th, 1998

Sorting by divide-and-conquer

NIST quicksort ;www.nist.gov/dads/HTML/quicksort.html

Science Fair Projects - Quicksort

Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, on average, needs Θ(n log n) comparisons to sort n items, while requiring Θ(n2) comparisons in the worst-case.

Quicksort's inner loop is such that it is usually easy to implement very efficiently on most computer architectures, which makes it significantly faster in practice than other Θ(n log n) algorithms that can sort in place or nearly so in the average case (recursively-implemented quicksort is not, as is sometimes regarded, an in-place algorithm, requiring Θ(log n) on average, and Θ(n) in the worst case, of stack space for recursion.)

Quicksort

Quicksort. Quicksort is a sorting algorithm that, on average, needs O(n log(n))
comparisons to sort n items. ... Quicksort was developed by CAR Hoare

[PDF] Microsoft PowerPoint - QuickSort

Quicksort Quicksort is one of the fastest and simplest sorting algorithms [Hoar]. It works recursively by a divide-and-conquer strategy

The Code Project - A QuickSort Algorithm With Customizable Swapping - C# Programming

Interactive Quicksort 1.1

Interactive Quicksort. Press "Start" to restart the algorithm. When you press the
"Start" button the algorithm will sort the numbers in the textfields. ...

Quicksort - C++, Java, Algorithm and Data Structure Tutorials by Denis Kulagin



Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: November 18, 2009