我是 Tcl 新手,为了学习,我正在尝试实现来自维基百科的合并排序算法伪代码:
function merge_sort(list m)
// if list size is 1, consider it sorted and return it
if length(m) <= 1
return m
// else list size is > 1, so split the list into two sublists
var list left, right
var integer middle = length(m) / 2
for each x in m before middle
add x to left
for each x in m after or equal middle
add x to right
// recursively call merge_sort() to further split each sublist
// until sublist size is 1
left = merge_sort(left)
right = merge_sort(right)
// merge the sublists returned from prior calls to merge_sort()
// and return the resulting merged sublist
return merge(left, right)
在我的 Tcl 脚本中,我这样做:
proc merge_sort { lst } {
if { [llength $lst] <= 1 } {
return $lst
}
set middle [expr {[llength $lst] / 2}]
set left [lrange $lst 0 $middle]
set right [lrange $lst [expr {$middle + 1}] [llength $lst]]
set left [merge_sort $left]
set right [merge_sort $right]
return [merge $left $right]
}
根据我的调试尝试,在递归调用merge_sort
proc 之前一切正常。
输出说我“堆栈空间不足(无限循环?) ”。老实说,我没有在代码中看到问题。我哪里错了?