Headline

The Quicksort sorting algorithm

Citation

(http://en.wikipedia.org/wiki/Quicksort, 21 April 2013)

Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.

Illustration

See the visualization of Quicksort on Wikipedia:

http://en.wikipedia.org/wiki/Quicksort

See various illustrations of Quicksort as available on YouTube, e.g.:

Quick-sort with Hungarian (Küküllőmenti legényes) folk dance

Quicksort in Haskell

-- Polymorphic sorting
sort :: Ord a => [a] -> [a]
sort [] = []
sort (pivot:rest) =
             (sort lesser)
          ++ [pivot]
          ++ (sort greater)
  where
    lesser  = filter (< pivot) rest
    greater = filter (>= pivot) rest

The first equation models that an empty list is sorted vacuously. The second equation picks the head of the list as the 'pivot' element, which is used to partition the input. Indeed, all elements 'lesser' than 'pivot' are collected in one helper list and all elements 'greater' (or equal) than 'pivot' are collected in another helper list. Quicksort is then invoked recursively on 'lesser' and 'greater' and the intermediate results are appended with the 'pivot' element in between.

The implementation is exercised as follows:

main = do
  let input = [2,4,3,1,4]
  print $ sort input -- [1,2,3,4,4]

Metadata


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.