几种递归算法

简单选择排序

int i,j;
for(i=0;i<n-1;i++){
	int min=i;
	for(j=i+1;j<n;j++){
		if(a[j]<a[min]) min=j;
	}
	swap(a[i],a[min]);
}
//非递归
void SelectSort(int a[],int i,int n){
	if(i==n-1) return ;
	int k=i;
	for(int j=i+1;j<n;j++){
		if(a[j]<a[k]){
			k=j;
		}
	}
	if(k!=i) swap(a[i],a[k]);
	SelectSort(a,i+1,n);
}
//递归

冒泡排序

for (int i = 1; i < n; i++){
	for (int j = 0; j <n - i; j++){
		if (a[j] > a[j + 1]){
			swap(a[j],a[j+1]);
		}
	}
}
//非递归
void BubbleSort(int a[],int i,int n){
	if(i==n) return ;
	for(int j=i;j<n;j++){
		if(a[j]>a[j+1]){
			swap(a[j],a[j+1]);
		}
	}
	BubbleSort(a,i,n-1);
}
//递归

快速排序

int partition(int a[],int s,int t){
	int i=s,j=t,temp=a[s];
	while(i!=j){
		while(j>i&&a[j]>temp) j--;
		a[i]=a[j];
		while(i<j&&a[i]<temp) i++;
		a[j]=a[i];
	}
	a[i]=temp;
	return i;
} 
void quicksort(int a[],int s,int t){
	if(s<t){
		int i=partition(a,s,t);
			quicksort(a,s,i-1);
			quicksort(a,i+1,t);
	} 
}

o(n)<时间复杂度<o(n2)
二路归并排序

void Merge(int a[],int low,int mid,int high){
	int *temp=(int *)malloc(sizeof(int)*(high-low+1));
	int i=low,j=mid+1,k=0;
	while(i<=mid&&j<=high){
		if(a[i]<=a[j]){
			temp[k++]=a[i++];
		}
		else{
			temp[k++]=a[j++];
		}
	}
	while(i<=mid) temp[k++]=a[i++];
	while(j<=high) temp[k++]=a[j++];
	for(k=0,i=low;i<=high;k++,i++) a[i]=temp[k];
	free(temp); 
}
//自下而上
void MergePass(int a[],int length,int n){//一趟二路归并 
	int i;
	for(i=0;i+2*length-1<n;i+=2*length){
		Merge(a,i,i+length-1,i+2*length-1);
	} 	
	if(i+length-1<n) Merge(a,i,i+length-1,n-1);
} 

void MergeSort(int a[],int n){
	int length=1;
	for(length=1;length<n;length*=2){
		MergePass(a,length,n);
	}
}

//自上而下
void MergeSort(int a[],int low,int high){
	int mid;
	if(low<high){
		mid=(low+high)/2;
		MergeSort(a,low,mid);
		MergeSort(a,mid+1,high);
		Merge(a,low,mid,high);
	}
}

时间复杂度为o(nlog2n)
求最大值和次大值

void solve(int a[],int low,int high,int &max1,int &max2){
	if(low==high){
		max1=a[low];
		max2=-INF;
	}
	else if(low==high-1){
		max1=max(a[low],a[high]);
		max2=min(a[low],a[high]);
	}
	else{
		int mid=(low+high)/2;
		int lmax1,lmax2,rmax1,rmax2;
		solve(a,low,mid,lmax1,lmax2);
		solve(a,mid+1,high,rmax1,rmax2);
		if(lmax1>lmax1){
			max1=lmax1;
			max2=max(lmax2,rmax1);
		}
		else if(lmax1<rmax1){
			max1=rmax1;
			max2=max(rmax2,lmax1);
		}
	}
}

二分查找

	int  left = 0;//左边
	int right = n-1;//右边
	while (left <= right)
	{
		int mid = (right + left) / 2;
		if (a[mid] > k)
		{
			right = mid - 1;
		}
		else if (a[mid] < k)
		{
			left = mid + 1;
		}
		else
		{
			printf("数值%d对应的下标为:%d\n",k,mid);
			break;
		}
	}
	if (left > right)
	{
		printf("不存在\n");
	}
//递归实现
void binSearch(int a[],int low,int high,int k,int &i){
	int mid;
	if(low<=high){
		mid=(low+high)/2;
		if(a[mid]==k){
		i=mid;
		return ;
		}
		else if(a[mid]>k) binSearch(a,low,mid-1,k,i);
		else binSearch(a,mid+1,high,k,i); 
	}
}

匈牙利算法

int M,N;//左右两侧的元素数量
int Map[MAXM][MAXN];//邻接矩阵 
int p[MAXN];//记录当前右侧元素对应的左侧元素 
bool vis[MAXN];//记录右侧元素是否被访问过 
bool match(int i){
	for(int j=1;j<=N;j++){
		if(Map[i][j]&&!vis[j]){
			vis[j]=true;
			if(p[j]==0||match(p[j])){
				p[j]=i;
				return true;
			}
		}
	}
	return false;
} 
int Hungarian(){
	int cnt=0;
	for(int i=1;i<=M;i++){
		memset(vis,0,sizeof(vis));
		if(match(i)) cnt++;
	}
	return cnt;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值