最近在看《算法导论》(2ed),趁机练练不是很熟悉的ruby~
#
!/usr/bin/env ruby
require ' Sort '
class PopoSort < Sort
@list = []
def sort(l)
@list = l
popoSort(@list)
putList(@list)
end
def popoSort(l)
a = l
i = 0
until i == a.length - 1
j = 0
until j == a.length - 1
if not a[j] < a[j + 1 ]
b = a[j + 1 ]
a[j + 1 ] = a[j]
a[j] = b
end
j += 1
end
i += 1
end
end
end
ms = PopoSort.new
# a = [2,4,3,1,2.5,0,6,100,24,64,18,72,100,88,99,88]
a = (0.. 1000 ).to_a
t1 = Time.new
ms.sort(a)
t2 = Time.new
puts " %.6f " % (t2.to_f - t1.to_f)
require ' Sort '
class PopoSort < Sort
@list = []
def sort(l)
@list = l
popoSort(@list)
putList(@list)
end
def popoSort(l)
a = l
i = 0
until i == a.length - 1
j = 0
until j == a.length - 1
if not a[j] < a[j + 1 ]
b = a[j + 1 ]
a[j + 1 ] = a[j]
a[j] = b
end
j += 1
end
i += 1
end
end
end
ms = PopoSort.new
# a = [2,4,3,1,2.5,0,6,100,24,64,18,72,100,88,99,88]
a = (0.. 1000 ).to_a
t1 = Time.new
ms.sort(a)
t2 = Time.new
puts " %.6f " % (t2.to_f - t1.to_f)