我想知道如果有人能帮助我理解如何为递归排序创建决策树。我知道如何处理,比如说冒泡排序或插入排序。但是,当涉及到递归排序时,我无法想象它。如果伪代码是一样的东西:递归排序算法的决策树
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
我显然会错了地方,我只是不太确定在哪里。我在正确的轨道上吗?
谢谢你给我的任何指针。
感谢您的快速回复。这不是功课;我一直在努力更好地理解算法,而我正在阅读的这本书目前正在讨论决策树。找到这个例子,并试图理解它:) –