Skip to content

Instantly share code, notes, and snippets.

@Momozono
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Momozono/28046779dc85341e9a9c to your computer and use it in GitHub Desktop.
Save Momozono/28046779dc85341e9a9c to your computer and use it in GitHub Desktop.
array = []
print "配列の数を指定してください:"; n = gets.chomp.to_i
print "最大の数を指定してください:"; r = gets.chomp.to_i
n.times do
array << rand(r)
end
count = array.length
def compare(left,right)
left <=> right
end
while count != 0
num1 = array.length - 1
num2 = num1 - 1
while num1 != 0
left = array[num2]
right = array[num1]
if compare(left,right) == 1 #戻り値1で左が大きい
array[num1], array[num2] = array[num2], array[num1]
end
num1 -= 1
num2 -= 1
end
count -= 1
end
print array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment