Created
October 9, 2008 09:58
-
-
Save be9/15740 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def quick(a) | |
return a if a.min == a.max | |
m = a[rand(a.size)] | |
quick( a.select { |i| i <= m } ) + quick( a.select { |i| i > m } ) | |
end | |
describe "Quicksort" do | |
it "should sort an empty array" do | |
quick([]).should == [] | |
end | |
it "should sort an array with size 1" do | |
quick([1]).should == [1] | |
end | |
it "should sort already sorted array" do | |
quick([1, 2, 3]).should == [1, 2, 3] | |
end | |
it "should sort unsorted array" do | |
quick([3, 2, 1]).should == [1, 2, 3] | |
end | |
it "should sort an array with same numbers" do | |
quick([1, 1, 1]).should == [1, 1, 1] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment