Created
June 18, 2013 23:51
-
-
Save kimihito/5810578 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/env ruby | |
#-*- coding: utf-8 -*- | |
def insert_sort(arr) | |
if arr.nil? | |
return arr | |
end | |
(arr.length).times do |i| | |
j = i | |
while j > 0 && arr[j-1] > arr[j] do | |
tmp = arr[j] | |
arr[j] = arr[j-1] | |
arr[j-1] = tmp | |
j -= 1 | |
end | |
end | |
arr | |
end | |
arr = Array.new(100) | |
100.times do |i| | |
arr[i] = rand(100) | |
end | |
puts insert_sort(arr) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment