Skip to content

Instantly share code, notes, and snippets.

@aghyad
Last active December 21, 2015 00:58
Show Gist options
  • Save aghyad/6223737 to your computer and use it in GitHub Desktop.
Save aghyad/6223737 to your computer and use it in GitHub Desktop.
Algorithms in Ruby: Quick Sort
class Array
def aghyad_quick_sort
return self if length <= 1
pivot = self[length/2]
return find_all { |i| i < pivot }.quick_sort + find_all { |i| i == pivot } + find_all { |i| i > pivot }.quick_sort
end
end
#############################################################################
# For example:
# [10,5,6,9,44,5555,66666,0,-5555555,5,6,6,6,6,6].aghyad_quick_sort
# OUTPUT >> [-5555555, 0, 5, 5, 6, 6, 6, 6, 6, 6, 9, 10, 44, 5555, 66666]
#############################################################################
@aghyad
Copy link
Author

aghyad commented Aug 13, 2013

How does quick sort work?
http://en.wikipedia.org/wiki/Quicksort

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