排序算法

1.基于顺序表为存储结构的直接插入排序、希尔排序、冒泡排序、快速排序、简单选择排序、堆排序

#include<iostream>
using namespace std;
//直接插入排序 
void insertSort(int r[],int n){
	for(int i=2;i<=n;i++){
		r[0]=r[i];
		int j=i-1;
		while(r[0]<r[j]) r[j+1]=r[j--];
		r[j+1]=r[0];
	}
}
//希尔排序 
void shellSort(int r[],int n){
	for(int d=n/2;d>=1;d/=2)
		for(int i=d+1;i<=n;i++){
			r[0]=r[i];
			int j=i-d;
			while(j>0&&r[0]<r[j]) {r[j+d]=r[j];j=j-d;}
			r[j+d]=r[0];
		}
}
//冒泡排序 
void bubbleSort1(int r[],int n){
	for(int i=1;i<n;i++)
		for(int j=n;j>i;j--){
			if(r[j]<r[j-1]){
				r[0]=r[j];
				r[j]=r[j-1];
				r[j-1]=r[0];
			}
		}
}
//改进的冒泡排序 
void bubbleSort2(int r[],int n){
	for(int i=1;i<n;i++){
		int noswap=1;
		for(int j=n;j>i;j--){
			if(r[j]<r[j-1]){
				r[0]=r[j];
				r[j]=r[j-1];
				r[j-1]=r[0];
				noswap=0;
			}
		}
		if(noswap) break;
	}
}
//最好的的冒泡排序
void bubbleSort3(int r[],int n){
	int exchange=n;
	while(exchange){
		int bound=exchange;
		exchange=0;
		for(int i=1;i<bound;i++)
			if(r[i]>r[i+1]){
				r[0]=r[i];
				r[i]=r[i+1];
				r[i+1]=r[0];
				exchange=i;
			}
	}
}
int position(int r[],int first,int end){
	r[0]=r[first];
	int i=first,j=end;
	while(i<j){
		while(i<j&&r[j]>=r[0]) j--;
		if(i<j)	r[i++]=r[j];
		while(i<j&&r[i]<=r[0]) i++;
		if(i<j) r[j--]=r[i];
	}
	r[i]=r[0];
	return i;
}
//快速排序 
void quickSort(int r[],int first,int end){
	if(first<end){
		int postion=position(r,first,end);
		quickSort(r,first,postion-1);
		quickSort(r,postion+1,end);
	}
}
//简单选择排序 
void selectSort(int r[],int n){
	for(int i=1;i<n;i++){
		int min=i;
		for(int j=i+1;j<=n;j++){
			if(r[j]<r[min]) min=j;
		}
		if(min!=i){
			r[0]=r[i];r[i]=r[min];r[min]=r[0];
		}
	} 	
}
//堆排序
void sift(int r[],int m,int n){
	int i=m,j=2*i;
	while(j<=n){
		if(j<n&&r[j]<r[j+1]) j++;
		if(r[i]>r[j]) break;
		else{
			r[0]=r[i];r[i]=r[j];r[j]=r[0];
			i=j;j=2*i;
		}
	}
} 
void heapSort(int r[],int n){
	for(int i=n/2;i>=1;i--)
		sift(r,i,n);
	for(int i=1;i<n;i++){
		r[0]=r[1];
		r[1]=r[n-i+1];
		r[n-i+1]=r[0];
		sift(r,1,n-i);
		cout<<"第"<<i<<"趟堆排序的结果:\n";
		for(int j=1;j<=n;j++)
			cout<<r[j]<<"  ";
		cout<<endl;
	}
}
void print(int r[],int n){
	for(int i=1;i<=n;i++)
		cout<<r[i]<<"  ";
	cout<<endl;
} 
int main(){
	int a[]={0,10,50,70,80,60,20,30,40,90,15};
	int b[]={0,85,55,20,70,35,40,65,80,30,25};
	int c1[]={0,95,45,25,75,85,20,15,50,90,70};
	int c2[]={0,88,65,33,85,99,23,81,76,82,42};
	int c3[]={0,114,580,670,599,470,571,245,456,440,250};
	int d[]={0,800,570,757,372,455,420,452,522,870,820};
	int e[]={0,12,48,29,77,85,28,17,50,71,52};
	int f[]={0,102,480,290,707,805,208,107,500,701,502};
	cout<<"排序前,a数组元素:\n";
	print(a,10);
	insertSort(a,10);
	cout<<"直接插入排序后,a数组元素:\n";
	print(a,10);
	cout<<"排序前,b数组元素:\n";
	print(b,10);
	shellSort(b,10);
	cout<<"希尔排序后,b数组元素:\n";
	print(b,10);
	cout<<"排序前,c1数组元素:\n";
	print(c1,10);
	bubbleSort1(c1,10);
	cout<<"冒泡排序法1排序后,c1数组元素:\n";
	print(c1,10);
	cout<<"排序前,c2数组元素:\n";
	print(c2,10);
	bubbleSort2(c2,10);
	cout<<"冒泡排序法2排序后,c2数组元素:\n";
	print(c2,10);
	cout<<"排序前,c3数组元素:\n";
	print(c3,10);
	bubbleSort2(c3,10);
	cout<<"冒泡排序法3排序后,c3数组元素:\n";
	print(c3,10);
	cout<<"排序前,d数组元素:\n";
	print(d,10);
	quickSort(d,1,10);
	cout<<"快速排序后,d数组元素:\n";
	print(d,10);
	cout<<"排序前,e数组元素:\n";
	print(e,10);
	quickSort(e,1,10);
	cout<<"简单排序后,e数组元素:\n";
	print(e,10);
	cout<<"排序前,f数组元素:\n";
	print(f,10);
	heapSort(f,10);
	cout<<"简单排序后,f组元素:\n";
	print(f,10);
	return 0;
} 

总结

在基于顺序表为存储结构的直接插入排序、希尔排序、冒泡排序、快速排序、简单选择排序、堆排序中,都是采用的从下标为1开始存储,同时将下标为0这个存储空间作为数据交换时的中间变量,所有不会造成浪费。同时,下标为0有时也作为哨所。对于问题的解决,我们不应该仅仅只满足于问题得到解决,还应该学会有不同的方法来使问题得到更高效,简便的解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值