TheAlgorithms-Ruby/sorting/pancake_sort.rb
Vitor Oliveira 8bb781f790 Minor fixes
2021-09-03 13:24:58 -07:00

19 lines
460 B
Ruby

def pancake_sort(array)
return array if array.length <= 1
(array.length - 1).downto(1) do |index|
max_index = array[0..index].index(array[0..index].max)
next if max_index == index
array[0..max_index] = array[0..max_index].reverse if max_index > 0
array[0..index] = array[0..index].reverse
end
array
end
if $0 == __FILE__
puts 'Enter a list of numbers separated by space'
list = gets.split.map(&:to_i)
p pancake_sort(list)
end