Skip to content

Instantly share code, notes, and snippets.

@Hoyasumii
Created August 23, 2024 14:47
Show Gist options
  • Save Hoyasumii/4afe8825b49ede09627176aa38c4761b to your computer and use it in GitHub Desktop.
Save Hoyasumii/4afe8825b49ede09627176aa38c4761b to your computer and use it in GitHub Desktop.
QuickSort
const data: Array<number> = [4, 20, 9, 74, 23, 100, 1, 5, 91];
function quicksort(data: Array<number>): Array<number> {
if (data.length < 2) {
return data;
}
const pivot: number = data[Math.floor(data.length / 2)];
const min: Array<number> = data.filter(item => item < pivot);
const max: Array<number> = data.filter(item => item > pivot);
return [ ...quicksort(min), pivot, ...quicksort(max) ];
}
console.time("data");
console.log(quicksort(data));
console.timeEnd("data");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment