Last active
September 29, 2015 09:40
-
-
Save theanam/22ab0e5f8114b22126c2 to your computer and use it in GitHub Desktop.
Quick Sort algorithm in JavaScript (with a relatively bigger memory complexity, for demonstration purpose only)
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
var unsorted = [53,12,22,87,74,67,23,45,77,1,8,44]; | |
function quickSort(arr){ | |
if(arr.length<2) | |
return arr; | |
var smaller = []; | |
var larger = []; | |
var pivot = arr[Math.floor(Math.random()*arr.length)]; | |
for(i=0;i<arr.length;i++){ | |
if(arr[i]<=pivot) | |
smaller.push(arr[i]) | |
else | |
larger.push(arr[i]) | |
} | |
return quickSort(smaller).concat(quickSort(larger)) | |
} | |
console.log(quickSort(unsorted)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment