Last active
December 18, 2015 14:58
-
-
Save kimihito/5800662 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
def select_sort(arr) | |
if arr.nil? | |
return arr | |
end | |
size = arr.length | |
(size-1).times do |i| | |
min_index = i | |
(i..size-1).each do |j| | |
if arr[j] < arr[min_index] | |
min_index = j | |
end | |
end | |
tmp = arr[i] | |
arr[i] = arr[min_index] | |
arr[min_index] = tmp | |
end | |
arr | |
end | |
arr = Array.new(100) | |
100.times do |i| | |
arr[i] = rand(100) | |
end | |
puts select_sort(arr) |
2013/06/19 添字i以降で一番小さな値を持つ添字を探すって行為が意味わかってなかった。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
insertって書いてたのを修正