我想知道是否有人可以帮助我了解如何为递归排序创建决策树。我了解如何使用冒泡排序或插入排序来做到这一点。但是,当涉及到递归排序时,我无法想象。如果伪代码类似于:
if length == 1
return;
else
int elem = head of array;
array tail = tail of array;
recursive sort;
if (elem <= head of sorted list)
add elem to the beginning of sorted list
else
swap elem and first element of sorted list
sort smaller list again
add elem to the beginning of sorted list
return
我最初的想法是决策树如下所示:
A, B, C
yes / \ no is length <= 1?
/ \
remove head
/ \
A B, C
yes / \ no is length <= 1?
/ \
remove head
/ \
B C
yes / \ no is length <= 1?
/ \
B:C
/ \
B,C C,B
| |
A:B,C A:C,B
/ \ / \
A,B,C B:A,C A,C,B C:A,B
/ \ / \
B,A,C A:B,C C,A,B A:C,B
我显然在某个地方出错了,我只是不太确定在哪里。我在正确的轨道上吗?
谢谢你能给我的任何指示。