Created
September 28, 2021 15:24
-
-
Save alexandr-kazakov/21b253a336f90fd00ba45515fed7f2c7 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, 8, 8, 2, 4, -100, -2, 4, 3, 2, 8, 100, 9, 1]; | |
function quickSort(array) { | |
if (array.length <= 1) return array; | |
let minIndex = 0; | |
let maxIndex = array.length - 1; | |
let middleArray = []; | |
let lessArray = []; | |
let moreArray = []; | |
let middleIndex = Math.floor(array.length / 2); | |
let middleElem = array[middleIndex]; | |
while (minIndex < maxIndex) { | |
if (array[minIndex] > middleElem) { | |
moreArray.push(array[minIndex]); | |
} else if (array[minIndex] == middleElem) { | |
middleArray.push(array[minIndex]); | |
} else if (array[minIndex] < middleElem) { | |
lessArray.push(array[minIndex]); | |
} | |
if (array[maxIndex] > middleElem) { | |
moreArray.push(array[maxIndex]); | |
} else if (array[maxIndex] == middleElem) { | |
middleArray.push(array[maxIndex]); | |
} else if (array[maxIndex] < middleElem) { | |
lessArray.push(array[maxIndex]); | |
} | |
++minIndex; | |
--maxIndex; | |
} | |
for (let i = 0; i < array.length; 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