Created
January 8, 2019 11:50
-
-
Save lsongdev/c7976c28f12d52d3719b6fd620072d2b 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
const quicksort = arr => { | |
if(arr.length <= 1) return arr; | |
const pivotIndex = arr.length / 2; | |
const pivot = arr.splice(pivotIndex, 1)[0]; | |
const left = []; | |
const right = []; | |
for(var i = 0; i < arr.length; i++){ | |
if(arr[i] < pivot){ | |
left.push(arr[i]); | |
}else{ | |
right.push(arr[i]); | |
} | |
} | |
return quicksort(left).concat([ pivot ], quicksort(right)); | |
}; | |
console.log(quicksort([ 22, 35, 27, 11, 9, 48, 16, 99, 2 ])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment