Skip to content

Instantly share code, notes, and snippets.

@alexandr-kazakov
Created September 28, 2021 15:09
Show Gist options
  • Save alexandr-kazakov/e3abe981c3255caf9991fbea4f2164c3 to your computer and use it in GitHub Desktop.
Save alexandr-kazakov/e3abe981c3255caf9991fbea4f2164c3 to your computer and use it in GitHub Desktop.
let array = [5, 5, 7, 2, 9, 1, 1, 1, -100, -2, 4, 3, 2, 8, 100, 9, 1];
function quickSort(array) {
if (array.length <= 1) return array;
let middleArray = [];
let lessArray = [];
let moreArray = [];
let middleIndex = Math.floor(array.length / 2);
let middleElem = array[middleIndex];
for (let i = 0; i < array.length; i++) {
if (array[i] > middleElem) {
moreArray.push(array[i]);
} else if (array[i] == middleElem) {
middleArray.push(array[i]);
} else {
lessArray.push(array[i]);
}
}
return [...quickSort(lessArray), ...middleArray, ...quickSort(moreArray)];
}
console.log(quickSort(array));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment