From: Robert Klemme Date: 2010-05-14T18:22:44+09:00 Subject: Re: Putting value in sorted array 2010/5/13 Sergey Chernov : > Gary Wright wrote: >> On Dec 8, 2007, at 3:27 PM, Jari Williamsson wrote: >>> Thanks for the tip! I optimized it into: >>> >>> arr = [5, 3, 1] >>> insert_value = 2 >>> arr.sort! { |a,b| b <=> a } if !(arr << insert_value).uniq! >> >> That is a lot of work to insert an item into a sorted array. > > sorting after each insertion is a bad idea. there is a quite simple > binary search that provide super effective way to insert into sorted > arrays. It could be done, for example, like this: Or this: http://raa.ruby-lang.org/project/ruby-bsearch/ Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/