Last active
January 22, 2016 10:26
-
-
Save sasanquaneuf/5e3e006a3009bae2b3d4 to your computer and use it in GitHub Desktop.
バケツソートとn log nの壁 - アルゴリズムクイックリファレンス4章の補足 - ref: http://qiita.com/sasanquaneuf/items/39d00e6f49d87643d472
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 bsort(a) # 非破壊的 | |
l = a.length | |
b = Array.new(l){0} | |
for i in 0..(l-1) do | |
b[a[i]] = a[i] | |
end | |
return b | |
end |
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
# 乱数列の作成 | |
timearr.push Time.now.instance_eval { self.to_i * 1000000 + usec} | |
N = 100000 | |
target = Array.new(N){0} | |
n = -1 | |
temp = Array.new(N){n;n+=1} | |
n += 1 | |
while n > 0 do | |
t = rand(n) | |
n -= 1 | |
target[n] = temp[t] | |
temp.delete_at(t) | |
end | |
targeta = target.clone | |
targetb = target.clone | |
timearr.push Time.now.instance_eval { self.to_i * 1000000 + usec} | |
bsort(targeta) | |
timearr.push Time.now.instance_eval { self.to_i * 1000000 + usec} | |
msort(targetb) | |
timearr.push Time.now.instance_eval { self.to_i * 1000000 + usec} | |
p timearr[1] - timearr[0] | |
p timearr[2] - timearr[1] | |
p timearr[3] - timearr[2] |
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
773380 # 準備の時間 | |
14539 # バケツソート(無重複)の時間 | |
253719 # マージソートの時間 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment