Skip to content

Instantly share code, notes, and snippets.

@ttezel
Created July 16, 2012 19:10
Show Gist options
  • Select an option

  • Save ttezel/3124434 to your computer and use it in GitHub Desktop.

Select an option

Save ttezel/3124434 to your computer and use it in GitHub Desktop.
Simple QuickSort in JS
function quickSort (arr) {
if (!arr.length)
return arr
var pivot = arr.splice(0, 1)
var less = []
var greater = []
arr.forEach(function (el) {
if (el <= pivot)
less.push(el)
else
greater.push(el)
})
return quickSort(less).concat(pivot, quickSort(greater))
}
var test = [ 1, 7, 3, 6, 8, 9, 10, 45, 23, 54, 4, 3 ]
var sorted = quickSort(test)
console.log('sorted', sorted)
@ttezel
Copy link
Copy Markdown
Author

ttezel commented Sep 25, 2021

Thanks for your kind words @DennyDaVjncj! :D

@zero-t4
Copy link
Copy Markdown

zero-t4 commented Sep 27, 2021

why did you splice arr, instead of taking random pivot index?

@DennyDaVjncj
Copy link
Copy Markdown

DennyDaVjncj commented Sep 28, 2021

Great question @zero-t4, I'm curious about that too

@Ian-kensington-chadwick-the-3rd
Copy link
Copy Markdown

your code is my study material in 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment