Skip to content

Instantly share code, notes, and snippets.

@brianxautumn
Created April 16, 2017 17:26
Show Gist options
  • Save brianxautumn/e4590666dda3c4a63b3db0d199f5edac to your computer and use it in GitHub Desktop.
Save brianxautumn/e4590666dda3c4a63b3db0d199f5edac to your computer and use it in GitHub Desktop.
function quicksort(input, low, high){
if(low < high){
var pivot = partition(input, low, high);
quicksort(input, low, pivot-1)
quicksort(input, pivot+1, high);
}
}
function partition(input, low, high){
var pivot = input[high];
var temp;
var i = low;
for(var j = low; j < high - 1; j++){
if(input[j] < pivot){
temp = input[i];
input[i] = input[j];
input[j] = temp;
i++;
}
}
temp = input[i + 1];
input[i + 1] = input[high];
input[high] = temp;
return i + 1;
}
var test = [1 , 5, 8 ,10, 45, 100, -5];
quicksort(test, 0 , test.length - 1)
console.log(test);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment