1
0
Fork 0
mirror of https://github.com/TheAlgorithms/Ruby synced 2025-01-29 20:34:27 +01:00
TheAlgorithms-Ruby/sorting/insertion_sort.rb

17 lines
409 B
Ruby
Raw Normal View History

2016-07-26 23:46:43 +05:30
def insertion_sort(array)
0.upto(array.length - 1).each do |index|
element = array[index]
position = index
while element < array[position - 1] && position > 0
array[position] = array[position - 1]
array[position - 1] = element
position -= 1
end
end
array
end
2020-11-01 20:58:09 +09:00
puts "Enter a list of numbers separated by space"
2016-07-26 23:46:43 +05:30
2020-05-14 11:15:53 +09:00
list = gets
2016-07-26 23:46:43 +05:30
insertion_sort(list)
print list