分治法取最大值的实现

str = [2,3,1,5,8,4,0,7,9,2,3,1,5,8,4,0,7,9,2,3,1,5,8,4,0,7,9,2,3,1,5,8,4,0,7,9,2,3,1,5,8,4,0,7,9]

def test1 arr
  len = arr.length
  (len%2==0)?(l = len/2):(l = (len+1)/2)
  ar = []
  for i in 0..l-1  
    if arr[i*2+1]!=nil and arr[i*2]<arr[i*2+1]
      ar<<arr[i*2+1]
    else
      ar<<arr[i*2]
    end
  end
  if ar.length!=1
    test1 ar
  else
    p ar
  end  
end

 

posted on 2013-09-12 11:46  张飞_  阅读(306)  评论(0编辑  收藏  举报

导航