Created
April 16, 2017 17:26
-
-
Save brianxautumn/e4590666dda3c4a63b3db0d199f5edac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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