Skip to content

Instantly share code, notes, and snippets.

@kimihito
Created June 18, 2013 23:51
Show Gist options
  • Save kimihito/5810578 to your computer and use it in GitHub Desktop.
Save kimihito/5810578 to your computer and use it in GitHub Desktop.
挿入ソート
#!/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