我使用 Quicksort 对整数进行排序,这些整数是由堆栈上的条目表示的集合中的元素。它可以正常工作,除非它必须对碰巧已经排序的更大(大约 10,000 个元素)集合进行排序。
: adswap \ ad1 ad2 --
over @ over @ swap rot ! swap ! ;
: singlepart \ ad1 ad2 -- ad
tuck 2dup @ locals| p ad | swap \ ad2 ad2 ad1
do i @ p < \ ad2 flag
if ad i adswap ad cell + to ad then cell \ ad2 cell
+loop ad adswap ad ; \ ad
: qsort \ ad1 ad2 -- pointing on first and last cell in array
2dup <
if 2dup singlepart >r
swap r@ cell - recurse
r> cell + swap recurse
else 2drop
then ;
它会在返回堆栈中溢出吗?程序几乎不可能跟踪数组何时排序,那么如何解决这个问题呢?