Skip to content

Instantly share code, notes, and snippets.

@be9
Created October 9, 2008 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save be9/15740 to your computer and use it in GitHub Desktop.
Save be9/15740 to your computer and use it in GitHub Desktop.
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