Skip to content

Instantly share code, notes, and snippets.

@kulicuu
Last active November 9, 2015 10:09
Show Gist options
  • Save kulicuu/f1a9021700a7feca9462 to your computer and use it in GitHub Desktop.
Save kulicuu/f1a9021700a7feca9462 to your computer and use it in GitHub Desktop.
better quicksort
c = -> console.log.apply console, arguments
# return array of size n, of random unsigned integers between 0 and a thousand
get_random_rayy = (n) ->
rayy = []
for i in [0 .. (n - 1)]
rayy.push Math.floor(Math.random() * 1000)
return rayy
module.exports = get_random_rayy
c = -> console.log.apply console, arguments
get_random_rayy = require('./make_random_array_.coffee')
quick_sort = (rayy) ->
len = rayy.length
if len is 1 or len is 0
return rayy
random_idx = Math.floor(Math.random() * len)
apple = rayy.splice random_idx, 1
pivot = apple[0]
less = []
more = []
for i, idx in rayy
if i < pivot
less.push i
if i >= pivot
more.push i
less_sorted = arguments.callee less
more_sorted = arguments.callee more
if less_sorted is undefined then less_sorted = []
if more_sorted is undefined then more_sorted = []
return less_sorted.concat(apple.concat(more_sorted))
rayy = get_random_rayy 11
c 'rayy', rayy.toString()
c quick_sort rayy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment