Skip to content

Instantly share code, notes, and snippets.

@monkyz
Created September 22, 2012 03:48
Show Gist options
  • Save monkyz/3765052 to your computer and use it in GitHub Desktop.
Save monkyz/3765052 to your computer and use it in GitHub Desktop.
function quicksort
Simple version
In simple pseudocode, the algorithm might be expressed as this:
function quicksort('array')
if length('array') ≤ 1
return 'array' // an array of zero or one elements is already sorted
select and remove a pivot value 'pivot' from 'array'
create empty lists 'less' and 'greater'
for each 'x' in 'array'
if 'x' ≤ 'pivot' then append 'x' to 'less'
else append 'x' to 'greater'
return concatenate(quicksort('less'), 'pivot', quicksort('greater')) // two recursive calls
--------------------------------------------------------------------------------------------------------
js version:
var quicksort = function (array){
if (array.length <= 1){
return array;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment