Insertion Sort Ruby

This commit is contained in:
Chetan Kaushik 2016-07-26 23:46:43 +05:30 committed by GitHub
parent 04d7fc3696
commit 420c7b76d0

17
InsertionSort.rb Normal file
View file

@ -0,0 +1,17 @@
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
puts "Enter a list of numbers seprated by space"
list = gets
insertion_sort(list)
print list