呵呵,我试试:
《数据结构》之所以采用递归完全是为了代码的简洁性和空间复杂度,但是时间复杂度可能会增加。如在N!的算法中:
int SORT_DG(int n)
{
if n==0 then return 1;
if n==1 then return 1;
if n>1 then return (n*SORT(n-1));
}
你看代码多简单,一目了源。
当我们采用栈后就需要附加的空间:
int SORT_Z(int n)
{
int MyResult=1;
if n>1 then
{
do (
MyResult=MyResult*n;
n--;
)while(n>0)
}
return MyResult;
}