数据结构(3)——递归算法

本文深入探讨了递归算法在阶乘计算、二分查找及排列组合中的应用,通过对比递归与迭代方法,详细解析了递归算法的实现原理与特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构(3)——递归算法(阶乘,二分查找,排列组合)

递归

  • 自己调用自己
  • 中间结果暂存
  • 代码简洁,易理解
  • 占用大量内存

eg:计算n的阶乘

#include <iostream>
using namespace std;
//-----递归 n!=n*(n-1)!------
long jiecheng(int n)
{
	if(n==0)
	return 1;
	else
	return n*jiecheng(n-1);
}
//-------迭代------- 
long jiecheng2(int n)
{
	long result=1;
	for(int i=n;i>0;i--)
	result=result*i;
	return result;
}
int main()
{
	for(int num=0;num<10;num++)
	{ 
	cout<<num<<"!="<<jiecheng(num)<<endl;  //递归结果
    cout<<"diedai"<<num<<"!="<<jiecheng2(num)<<endl;//迭代结果
	}
	return 0;
}

eg:二分查找

#include <iostream>
using namespace std;
int BinarySearch_1(int *a,const int x,const int n);
int BinarySearch_2(int *a,const int x,const int left,const int right);
int main()
{
	int m[]={1,2,3,4,5,6,7,8,9,10};
	int result;
	int num=7;
	if((result=BinarySearch_2(m,num,0,9))<0)
	{
		cout<<"digui can't find"<<endl;
	}
	else
	{
		cout<<"digui inm["<<result<<"]can find"<<num<<endl;
	}//递归 
	if((result=BinarySearch_1(m,num,10))<0)
	{
		cout<<"diedai can't find"<<endl;
	}
	else
	{
		cout<<"diedai inm["<<result<<"]can find"<<num<<endl;
	}//迭代 
	return 0;
}
//--------迭代二分查找-----------
int BinarySearch_1(int *a,const int x,const int n)
{
	int left=0,right=n-1;
	while (left<=right)
	{
		int middle=(left+right)/2;
		if(x<a[middle]) right=middle-1;
		else if(x>a[middle]) left=middle+1;
		else return middle;
	}
	return -1;
}
//---------递归二分查找-----------
int BinarySearch_2(int *a,int x,const int left,const int right)
{
	if(left<=right)
	{
		int middle=(left+right)/2;
		if(x<a[middle]) return BinarySearch_2(a,x,left,middle-1);
		else if(x>a[middle]) return BinarySearch_2(a,x,middle+1,right);
		else return middle; 
	}
	return -1;
}

eg:排列组合

  • 递归调用10次
  • 返回9次
#include <iostream>
using namespace std;
/*
int c1=0;        //查看调用
int c2=0;        //查看返回
*/
/*void show(char*p,int m)       //show函数
{
    for(int i=0;i<=m;i++)
        cout<<p[i];
    cout<<endl;
}*/
void Permutations(char*p,const int k,const int m)   //递归函数 
{
    /*cout<<"c1="<<++c1<<endl;//输出查看调用次数 */
	if(k==m)                             
	{
		for(int i=0;i<=m;i++)
		cout<<p[i];
		cout<<endl;
	 } 
	else
	 {
	  for(int i=k;i<=m;i++)
	     {
          /*cout<<"递归前,交换前:";
          show(p,m);*/
		swap(p[k],p[i]);
          /*cout<<"递归前,交换后:";
          show(p,m);*/
		Permutations(p,k+1,m);
          /*cout<<"c2="<<++c2<<endl;   //输出查看返回次数
          cout<<"递归后,交换前:";
          show(p,m);*/
		swap(p[k],p[i]);
          /*cout<<"递归后,交换后:";
          show(p,m);*/
	    }
	 }
/*	
//-------交换过程显示-------
	swap(p[0],p[0]);
	Permutations(p,1,2);
	swap(p[0],p[0]);						//以a开头,bc的所有排列形式 
	
	swap(p[0],p[1]);
	Permutations(p,1,2;)							
	swap(p[0],p[1]);                       //以b开头,ac的所有排列形式
	
	swap(p[0],p[2]);
	Permutations(p,1,2);							
	swap(p[0],p[2]);                      //以c开头,ab的所有排列形式
*/ 
}
int main()
{
	char s[]="abc";
	Permutations(s,0,2); 
	system("pause");           //内函数,暂停 
	return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值