Skip to content

Instantly share code, notes, and snippets.

@sdymj84
Last active December 20, 2019 17:53
Show Gist options
  • Save sdymj84/2faf220c6cd1556e0a1976479b58cb3c to your computer and use it in GitHub Desktop.
Save sdymj84/2faf220c6cd1556e0a1976479b58cb3c to your computer and use it in GitHub Desktop.
[JS] QuickSort Algorithm with random array
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