Find the median of an array

前端 未结 4 1581
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 17:25

I wrote some code that returns the median of an unsorted odd numbered array, but it does not return the median of an even numbered array.

I know that in order to find

4条回答
  •  长情又很酷
    2021-01-25 18:20

      a = [1,2,4,5,3,6]
      sorted = a.sort
      list = sorted.length
      if list %2 != 0
        puts sorted[list/2]
      else
        puts (sorted[list/2]+sorted[(list/2)-1])/2.0
      end
    

提交回复
热议问题