【发布时间】:2018-06-19 21:35:36
【问题描述】:
我正在尝试实现合并排序算法。我有以下代码:
def merge_sort(array)
if array.length < 2
return array
else
length = array.length
i = array[0..array.length/2-1]
j = array[array.length/2 .. -1]
first = merge_sort(i)
second = merge_sort(j)
sorted_array = []
until first.empty? || second.empty? do
if first[0] >= second[0]
sorted_array << second.shift
else
sorted_array << first.shift
end
end
end
end
我收到了NoMethodError for NilClass。
据我了解,unless 块应该检查空数组,并在 Nil 类发生之前停止执行。
为什么会出现这个错误?
【问题讨论】:
-
这不会回答你的问题,但你可以试试看here
-
你没有
unless块。
标签: ruby mergesort nomethoderror