mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-17 06:11:43 +01:00
Insertion Sort Ruby
This commit is contained in:
parent
04d7fc3696
commit
420c7b76d0
1 changed files with 17 additions and 0 deletions
17
InsertionSort.rb
Normal file
17
InsertionSort.rb
Normal 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
|
Loading…
Reference in a new issue