Skip to content

Commit

Permalink
Benchmarks for Sort algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
kanwei committed Sep 16, 2008
1 parent adb0ae4 commit 7107313
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions benchmarks/sorts.rb
@@ -0,0 +1,34 @@
$: << File.join(File.expand_path(File.dirname(__FILE__)), '../lib')
require 'algorithms'
include Algorithms

require 'rubygems'
require 'rbench'

RBench.run(5) do

sorts = %w(ruby comb_sort heapsort insertion_sort shell_sort quicksort mergesort)
sorts.each { |sort| self.send(:column, sort.intern) }

n = 1000

proc = lambda { |scope, ary|
scope.ruby { ary.dup.sort }
scope.comb_sort { Sort.comb_sort(ary.dup) }
scope.heapsort { Sort.heapsort(ary.dup) }
scope.insertion_sort { Sort.insertion_sort(ary.dup) }
scope.shell_sort { Sort.shell_sort(ary.dup) }
scope.quicksort { Sort.quicksort(ary.dup) }
scope.mergesort { Sort.mergesort(ary.dup) }
}

report "Already sorted" do
sorted_array = Array.new(n) { rand(n) }.sort
proc.call(self, sorted_array)
end

report "Random" do
random_array = Array.new(n) { rand(n) }
proc.call(self, random_array)
end
end

0 comments on commit 7107313

Please sign in to comment.