Last active
December 21, 2015 00:58
-
-
Save aghyad/6223737 to your computer and use it in GitHub Desktop.
Algorithms in Ruby: Quick Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
############################################################################# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How does quick sort work?
http://en.wikipedia.org/wiki/Quicksort