排序算法

对输入的n个整数进行排序
输入样例
9
4 3 5 6 1 9 8 2 7
自带的排序算法–从小到大进行排序

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int *a=new int [n+1];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	sort(a+1,a+n);//从小到大进行排序 
	cout<<endl;
	system("pause");
	return 0;
}

从到大小进行排序

#include<iostream>
#include<algorithm>
using namespace std;
int my_comp(const int &a, const int &b)
{
	return a>b;
}
int main()
{
	int n;
	cin>>n;
	int *a=new int [n+1];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	sort(a+1,a+n,my_comp);//从大到小进行排序 
	for(int i=1;i<=n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	system("pause");
	return 0;
}

选择排序
相当于找第一小 第二小 然后分别与第一位 第二位所在的数进行交换

#include<iostream>
using namespace std;
void selectsort(int *r,int n)//选择排序 
{
	for(int i=1;i<=n-1;i++)
	{
		int k=i;
		for(int j=i+1;j<=n;j++)
		{
			if(r[j]<r[k])k=j;
		}
		if(k!=i)
		{
			int temp=r[k];
			r[k]=r[i];
			r[i]=temp;
		}
	}
}
int main()
{
	int n;
	cin>>n;
	int *r=new int[n+1] ;
	for(int i=1;i<=n;i++)
		cin>>r[i];
	selectsort(r,n);
	for(int i=1;i<=n;i++)
		cout<<r[i]<<" ";
	cout<<endl;
	delete []r;
	system("pause");
	return 0;
}

冒泡排序
与相邻的数字进行大小比较

#include<iostream> 
using namespace std;
void bubblesort(int *r,int n)
{
	for(int i=1;i<=n-1;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			if(r[i]>r[j])
			{
				int temp=r[i];
				r[i]=r[j];
				r[j]=temp;
			}
		}
	}
}
int main()
{
	int n;
	cin>>n;
	int *r=new int[n+1] ;
	for(int i=1;i<=n;i++)
		cin>>r[i];
	bubblesort(r,n);
	for(int i=1;i<=n;i++)
		cout<<r[i]<<" ";
	cout<<endl;
	delete []r;
	system("pause");
	return 0;
}

冒泡排序改进版

#include<iostream> 
using namespace std;
void bubblesort(int *a,int n)
{
	int i=1;
	bool bo;
	do
	{
		bo=true;
		for(int j=1;j<=n-i;j++)
		{
			if(a[j]>a[j+1])
			{
				int temp=a[j];
				a[j]=a[j+1];
				a[j+1]=temp;
				bo=false;
			}
		}
		i++;
	}while(!bo);
}
int main()
{
	int n;
	cin>>n;
	int *r=new int[n+1] ;
	for(int i=1;i<=n;i++)
		cin>>r[i];
	bubblesort(r,n);
	for(int i=1;i<=n;i++)
		cout<<r[i]<<" ";
	cout<<endl;
	delete []r;
	system("pause");
	return 0;
}

插入排序
将遍历到的前面的数字作为一个整体进行调整

#include<iostream>
using namespace std;
void insertsort(int *r,int n)
{
	int i=0,j=0,x=0;
	for(i=2;i<=n;i++)
	{
		x=r[i];j=i-1;
		while(x<r[j])
		{
			r[j+1]=r[j];
			j--;
		}
		r[j+1]=x;
	}
}
int main()
{
	int n;
	cin>>n;
	int *r=new int[n+1] ;
	for(int i=1;i<=n;i++)
		cin>>r[i];
	insertsort(r,n);
	for(int i=1;i<=n;i++)
		cout<<r[i]<<" ";
	cout<<endl;
	delete []r;
	system("pause");
	return 0;
}

桶排序

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int b[101],n,i,j,k;
	memset(b,0,sizeof(b));
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>k;
		b[k]++;
	}
	for(i=0;i<=100;i++)
	{
		while(b[i]>0)
		{
			cout<<i<<" ";
			b[i]--;
		}
	}
	cout<<endl;
	system("pause");
	return 0;
}

快速排序

#include<iostream> 
using namespace std;
void qsort(int *a,int l,int r)
{
	int i,j,mid,p;
	i=l;j=r;
	mid=a[(l+r)/2];
	do
	{
		while(a[i]<mid)i++;
		while(a[j]>mid)j--;
		if(i<=j)
		{
			p=a[i];
			a[i]=a[j];
			a[j]=p;
			i++;
			j--;
		}
	}while(i<=j);
	if(i<r)qsort(a,i,r);
	if(l<j)qsort(a,l,j);
}
int main()
{
	int n;
	cin>>n;
	int *r=new int [n+1];
	for(int i=1;i<=n;i++)
	cin>>r[i];
	qsort(r,1,n);
	for(int i=1;i<=n;i++)
	cout<<r[i]<<" ";
	cout<<endl;
	system("pause") ;
	return 0;
} 

归并排序

#include<iostream> 
using namespace std;
void mergesort(int *a,int s,int t)
{
	int *r=new int[t+1];
	for(int i=1;i<=t;i++)
	r[i]=0;
	int m,i,j,k;
	if(s==t)return ;
	m=(s+t)/2;
	mergesort(a,s,m);
	mergesort(a,m+1,t);
	i=s;j=m+1;k=s;
	while(i<=m&&j<=t)
	{
		if(a[i]<=a[j])
		{
			r[k]=a[i];
			k++;
			i++;
		}
		else
		{
			r[k]=a[j];
			j++;
			k++;
		}
	}
	while(i<=m)
	{
		r[k]=a[i];
		i++;
		k++;
	}
	while(j<=t)
	{
		r[k]=a[j];
		j++;
		k++;
	}
	for(i=s;i<=t;i++)
	{
		a[i]=r[i];
	}
	delete []r;
}
int main()
{
	int n;
	cin>>n;
	int *r=new int[n+1];
	for(int i=1;i<=n;i++)
	{
		cin>>r[i];
	}
	mergesort(r,1,n);
	for(int i=1;i<=n;i++)
	cout<<r[i]<<" ";
	cout<<endl;
	delete[]r;
	system("pause");
	return 0;
}

以后这方面肯定还需要有所改进,留待日后再续.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值