Skip to content

Instantly share code, notes, and snippets.

@ttezel
Created July 16, 2012 19:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttezel/3124434 to your computer and use it in GitHub Desktop.
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)
@DennyDaVjncj
Copy link

wow, your code is my study material in 2021! keep up the good work bruv!

@ttezel
Copy link
Author

ttezel commented Sep 25, 2021

Thanks for your kind words @DennyDaVjncj! :D

@zero-t4
Copy link

zero-t4 commented Sep 27, 2021

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

@DennyDaVjncj
Copy link

DennyDaVjncj commented Sep 28, 2021

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

@Ian-kensington-chadwick-the-3rd

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