Created
September 28, 2021 15:09
-
-
Save alexandr-kazakov/e3abe981c3255caf9991fbea4f2164c3 to your computer and use it in GitHub Desktop.
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
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