RosettaCodeData/Task/Order-by-pair-comparisons/Ruby/order-by-pair-comparisons-1.rb
2023-07-01 13:44:08 -04:00

13 lines
444 B
Ruby

items = ["violet", "red", "green", "indigo", "blue", "yellow", "orange"]
count = 0
sortedItems = []
items.each {|item|
puts "Inserting '#{item}' into #{sortedItems}"
spotToInsert = sortedItems.bsearch_index{|x|
count += 1
print "(#{count}) Is #{item} < #{x}? "
gets.start_with?('y')
} || sortedItems.length # if insertion point is at the end, bsearch_index returns nil
sortedItems.insert(spotToInsert, item)
}
p sortedItems