快速排序、冒泡排序、选择排序、插入排序

#include<iostream>
using namespace std;

int Partition(int a[],int left,int right){
	int temp=a[left];
	while(left<right){
		while(right>left && a[right]>=temp)  right--;
		a[left]=a[right];
		while(left<right && a[left]<=temp)   left++;
		a[right]=a[left];
	}
	a[left]=temp;
	return left;
}

void quicksort(int a[],int left,int right){
	if(left<right){
	int pos=Partition(a,left,right);
	quicksort(a,left,pos-1);
	quicksort(a,pos+1,right);
	}
}

void bubble_sort(int a[],int n){
	int temp;
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			if(a[i]>a[j]){
				a[j]=temp;
				a[j]=a[i];
				temp=a[i];
			}
		}
	}
}

void select_sort(int a[],int n){

	for(int i=0;i<n;i++){
		int k=i;
		for(int j=i;j<n;j++){
			if(a[k]>a[j])
				k=j;
		}
		int temp=a[i];
		a[i]=a[k];
		a[k]=temp;
	}
}

void insertsort(int a[],int n){
	for(int i=1;i<n;i++){
		int temp=a[i];
		int j=i;
		while(j>0 && temp<a[j-1]){
			a[j]=a[j-1];
			j--;
		}
		a[j]=temp;
	}
}

void main(){
	int a[11]={35,18,16,72,24,65,12,88,46,28,55};
	quicksort(a,0,10);
	for(int i=0;i<11;i++)
		cout<<a[i]<<" ";
	cout<<endl;

	bubble_sort(a,11);
	for(int j=0;j<11;j++)
		cout<<a[j]<<" ";
	cout<<endl;

	select_sort(a,11);
	for(int m=0;m<11;m++)
		cout<<a[m]<<" ";
	cout<<endl;

	insertsort(a,11);
	for(int n=0;n<11;n++)
		cout<<a[n]<<" ";
	cout<<endl;



}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值