Skip to content

Instantly share code, notes, and snippets.

@rajatk16
Created December 18, 2018 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rajatk16/1ee2794137762981ced65c6930a1d122 to your computer and use it in GitHub Desktop.
Save rajatk16/1ee2794137762981ced65c6930a1d122 to your computer and use it in GitHub Desktop.
quickSort = (array) => {
if (array.length < 2) {
return array
}
const chosenIndex = array.length - 1
const chosen = array[chosenIndex]
const a = []
const b = []
for (let i = 0; i < chosenIndex; i++) {
const temp = array[i]
temp < chosen ? a.push(temp) : b.push(temp)
}
const output = [...quickSort(a), chosen, ...quickSort(b)]
console.log(output.join(' '))
return output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment