Skip to content

Instantly share code, notes, and snippets.

@mindplace
Last active February 28, 2016 23:00
Show Gist options
  • Save mindplace/8530df7c15e92845d744 to your computer and use it in GitHub Desktop.
Save mindplace/8530df7c15e92845d744 to your computer and use it in GitHub Desktop.
def quick_sort(array, pivot=-1)
if array.empty? || array.length == 1
return array
end
slice_left = []
slice_right = []
pivot_slices = []
array.each_index do |j|
next if j == pivot
slice_left << array[j] if array[j] < array[pivot]
slice_right << array[j] if array[j] > array[pivot]
pivot_slices << array[j] if array[j] == array[pivot]
end
p quick_sort(slice_left) + pivot_slices + [array[pivot]] + quick_sort(slice_right)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment