所以我有以下代碼在Ruby中進行合併排序。系統堆棧錯誤
class MergeSort
def sort(array)
if array.length == 1 || array.length == 0
return array
end
firstHalf = array[0..array.length/2]
secondHalf = array[(array.length/2) + 1..array.length]
firstHalf = sort(firstHalf)
secondHalf = sort(secondHalf)
b = 0
c = 0
for i in (0..(firstHalf.length - 1))
while b < secondHalf.length && firstHalf[i] >= secondHalf[b]
array[c] = secondHalf[b]
b = b + 1
c = c + 1
array[c] = firstHalf[i]
c = c + 1
end
return array
end
end
array = [1,4,9,14,20,25]
puts MergeSort::new.sort(array)
當我運行代碼時,我得到一個SystemStackError
。有人能告訴我爲什麼會發生這種情況嗎?謝謝。
它不引發SystemStackError。它引發了語法錯誤。 – sawa