【算法】递增序列

给定n个整数序列<a1,a2,......,an>,输出该序列的一个排列 <a1’,a2’,......,an’>,满足a1’<=a2’<=......<=an’。

 

解题思路:

//归并排序的递归 非递归   ,快速排序的 递归 随机化 
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

void Display(int a[],int n);
void Display(int a[],int n){
	for(int i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
}
// 归并排序 
//递归
void Merge(int a[] , int b[],int l,int x,int r);
void MergeSort1(int a[],int left,int right);
void Copy(int a[],int b[],int left,int right);

void MergeSort1(int a[],int left,int right){
	int b[100];
	if(left < right){
		int i=(left + right)/2;
		MergeSort1(a,left,i);
		MergeSort1(a,i+1,right);
		Merge(a,b,left,i,right);
		Copy(a,b,left,right);
	}	
} 
void Merge(int a[] , int b[],int l,int x,int r){
	int i=l, j=x+1,k=l;
	while((i<=x)&&(j<=r)){
		if(a[i]<=a[j])
			b[k++] =a[i++];
		else b[k++]=a[j++];
		if(i>x){
			for(int y=j;y<=r;y++)
				b[k++]=a[y];
		}
		else if(j>r){
			for(int y=i;y<=x;y++)
				b[k++]=a[y];
		}
	}
}
void Copy(int a[],int b[],int left,int right){
	if(left<=right){
		for(int i=left;i<=right;i++)
			a[i]=b[i];
	}
}
//非递归
void MergeSort2(int a[],int n);
void MergePass(int a[],int b[],int s,int n);

void MergeSort2(int a[],int n){
	int *b=new int[n];
	int s=1;
	while(s<n){
		MergePass(a,b,s,n);
		s+=s;
		MergePass(a,b,s,n);
		s+=s;
	}
} 
void MergePass(int a[],int b[],int s,int n){
	int i=0;
	while(i<=n-2*s){
		Merge(a,b,i,i+s-1,i+2*s-1);
		i+=(2*s);
	}
	if(i+s<n)
		Merge(a,b,i,i+s-1,n-1);
	else
		for(int j=i;j<=n-1;j++)
			b[j]=a[j];
	Copy(a,b,0,n-1);
}
//快速排序
//递归
void QuickSort(int a[],int p,int r);
int Partition(int a[],int p,int r);
void Swap(int &x,int &y);
void QuickSort(int a[],int p,int r){
	if(p<r){
		int q=Partition(a,p,r);
		QuickSort(a,p,q-1);
		QuickSort(a,q+1,r);
	}
}  
int Partition(int a[],int p,int r){
	int i=p,j=r+1;
	int x=a[p];
	while(true){
		while(a[++i]<x && i<r) ;
		while(a[--j]>x) ;
		if(i>=j)
			break;
		Swap(a[i],a[j]);
	}
	a[p]=a[j];
	a[j]=x;
	return j;
}

void Swap(int &x,int &y){
	int a=x;
	x=y;
	y=a;
}
//随机化
int RandPartition(int a[],int p,int r);
void RandQuickSort(int a[],int p,int r);
int Random(int x,int y);

int Random(int x,int y){
	int i=x,j=y;
	int temp=rand()%(j-i+1)+i;
	return temp;
	
}
int RandPartition(int a[],int p,int r){
	int i=Random(p,r);
	Swap(a[i],a[p]);
	return Partition(a,p,r);
}
void RandQuickSort(int a[],int p,int r){
	if(p<r){
		int q=RandPartition(a,p,r);
		RandQuickSort(a,p,q-1);
		RandQuickSort(a,q+1,r);
	}
}
int main(){
	int n, a[100];
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i];
	int b[100],c[100],d[100];
	for(int i=0;i<n;i++){
		b[i]=a[i];
		c[i]=a[i];
		d[i]=a[i];
	}
	cout<<"递归合并排序:"<<endl;
	MergeSort1(a,0,n-1);
	Display(a,n);
	cout<<"非递归合并排序:"<<endl;
	MergeSort2(b,n); 
	Display(b,n);
	cout<<"递归快速排序:"<<endl;
	QuickSort(c,0,n-1);
	Display(c,n);
	cout<<"随机化快速排序:"<<endl; 
	RandQuickSort(d,0,n-1);
	Display(d,n);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值