Last active
December 20, 2019 17:53
-
-
Save sdymj84/2faf220c6cd1556e0a1976479b58cb3c to your computer and use it in GitHub Desktop.
[JS] QuickSort Algorithm with random array
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 qsort(array) { | |
if (array.length <= 1) { | |
return array; | |
} | |
const left = []; | |
const right = []; | |
const pivot = array.pop(); | |
for (let i = 0; i < array.length; i++) { | |
if (array[i] < pivot) { | |
left.push(array[i]); | |
} else { | |
right.push(array[i]); | |
} | |
} | |
return [...qsort(left), pivot, ...qsort(right)]; | |
} | |
function generateArray() { | |
const array = []; | |
for (let i = 0; i < Math.floor(Math.random() * 10 + 2); i++) { | |
array.push(Math.floor(Math.random() * 20)); | |
} | |
return Array.from(new Set(array)); | |
} | |
for (let i=0 ; i<200 ; i++) { | |
console.log(qsort(generateArray())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment