Skip to content

Instantly share code, notes, and snippets.

@paulohfev
Created April 24, 2025 18:44
Show Gist options
  • Select an option

  • Save paulohfev/ba8b493ff0a0066c05d1d1126ab8a050 to your computer and use it in GitHub Desktop.

Select an option

Save paulohfev/ba8b493ff0a0066c05d1d1126ab8a050 to your computer and use it in GitHub Desktop.
Quicksort using recursion
function quicksort(list) {
if (list.length < 2) {
return list
} else {
let pivot = list[0]
let restOfList = list.slice(1)
let listGreaterThanPivot = []
let listSmallerThanPivot = []
for (let i = 0; i < restOfList.length; i++) {
if (restOfList[i] > pivot) {
listGreaterThanPivot.push(restOfList[i])
} else {
listSmallerThanPivot.push(restOfList[i])
}
}
return quicksort(listSmallerThanPivot).concat([pivot], quicksort(listGreaterThanPivot));
}
}
console.log('sort', quicksort([1, 15, 10, 33, 2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment