Skip to content

Instantly share code, notes, and snippets.

@mateja176
Created December 23, 2024 17:57
Show Gist options
  • Save mateja176/46911d1f0296279bf0084dad2710c9ef to your computer and use it in GitHub Desktop.
Save mateja176/46911d1f0296279bf0084dad2710c9ef to your computer and use it in GitHub Desktop.
const quickSort = (a: Array<number>): Array<number> => {
if (a.length <= 1) {
return a
}
const [n, ...ns] = a
const { left, right } = ns.reduce<{ left: Array<number>; right: Array<number> }>(
({ left: left1, right: right1 }, m) => {
if (m > n) {
return { left: left1, right: right1.concat(m) }
}
return { left: left1.concat(m), right: right1 }
},
{ left: [], right: [] },
)
return [...quickSort(left), n, ...quickSort(right)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment