Created
December 18, 2015 03:07
-
-
Save yamaguchi1024/44fbe726f51669e56000 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/ruby | |
require 'openssl' | |
class N2Sort | |
def initialize(n) | |
$a=Array.new(n) | |
end | |
def fill_reverse(n) | |
initialize(n) | |
n.times do |i| | |
$a[i]=n-1 | |
end | |
end | |
def fill_random(n) | |
initialize(n) | |
n.times do |i| | |
$a[i]=rand(1000000000) | |
end | |
end | |
def sort(n) | |
(0..n-1).each { |i| | |
z=$a[0] | |
(0..n-2-i).each { |j| | |
x,y = z,$a[j+1] | |
if x>y then | |
$a[j],z = y,x | |
else | |
$a[j],z = x,y | |
end | |
} | |
$a[n-1-i]=z | |
} | |
end | |
def verify(n) | |
(0..n-2).each { |i| | |
if($a[i]>$a[i+1]) then | |
return false | |
end | |
} | |
return true | |
end | |
def measure(n) | |
$log=0 | |
#fill_random(n) | |
fill_reverse(n) | |
start_time=Time.now | |
sort(n) | |
end_time=Time.now | |
elapsed_time=(end_time-start_time).to_f() | |
return verify(n)?elapsed_time:Float::NAN | |
end | |
end | |
def run | |
nn=[500,2000,4000,6000,8000,10000] | |
s=N2Sort.new(nn[-1]) | |
$cookies=false | |
nn.each { |n| | |
$nloops=(nn[-1]/n).to_i() | |
$n=n | |
print n.to_s()+"" | |
5.times { | |
print "\t" | |
print s.measure(n) | |
} | |
print "\n" | |
} | |
end | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment