Created
November 10, 2016 18:01
-
-
Save jameslockwood/ee5f40258ef3ff3b5c367584b6f9eb99 to your computer and use it in GitHub Desktop.
Quicksort implementation using 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 <= 1) { | |
return arr; | |
} | |
let left = [], right = [], same = []; | |
let mid = arr[Math.floor((arr.length - 1) / 2)]; | |
for (let value of arr) { | |
if (value === mid) { | |
same.push(value); | |
} else if (value < mid) { | |
left.push(value); | |
} else { | |
right.push(value); | |
} | |
} | |
return [...quickSort(left), ...same, ...quickSort(right)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment