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)
@DennyDaVjncj

Copy link
Copy Markdown

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

@ttezel

ttezel commented Sep 25, 2021

Copy link
Copy Markdown
Author

Thanks for your kind words @DennyDaVjncj! :D

@zero-t4

zero-t4 commented Sep 27, 2021

Copy link
Copy Markdown

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

@DennyDaVjncj

DennyDaVjncj commented Sep 28, 2021

Copy link
Copy Markdown

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