Last active
June 1, 2017 10:41
-
-
Save reciosonny/062ec008e461938b39ce5abe6c973ac5 to your computer and use it in GitHub Desktop.
Quicksort in javascript
This file contains 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(arr) { | |
if (arr.length < 2) { | |
return arr; | |
} else { | |
var pivot = arr[0]; | |
var less = []; | |
var greater = []; | |
var result = []; | |
for (var i = 1; i < arr.length; i++) { //skip first array | |
var j = arr[i]; | |
if (j <= pivot) { | |
less.push(j); | |
} else if (j > pivot) { | |
greater.push(j); | |
} | |
} | |
return quicksort(less) + [pivot] + quicksort(greater); | |
} | |
} | |
console.log(quicksort([10,5,2,3,7,4])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment