内排序中3种复杂度为(n^2)的算法-----插入、冒泡、选择


#include <iostream>
using namespace std;
void swap(int &a,int &b)
{
	int t;
	t=a;
	a=b;
	b=t;
}
void show(int *a,int length)
{
	int i;
	for (i=0;i<length;i++)
		cout<<a[i]<<" ";
	cout<<endl;
}

void Insert_Sort(int *a,int length)
{
	int i,j;
	cout<<"......using insert_sort....."<<endl;
	for(i=1;i<length;i++)
		for(j=i;j>0;j--)
		{
			if(a[j]<a[j-1])
				swap(a[j-1],a[j]);
		}
}

void Bubble_Sort(int *a,int length)
{
	int i,j;
	cout<<"......using bubble_sort....."<<endl;
	for(i=0;i<length;i++)
		for(j=i;j<length-1;j++)
		{
			if(a[j]>a[j+1])
				swap(a[j],a[j+1]);
		}
}

void Selection_Sort(int *a,int length)
{
	int i,j;
	cout<<"......using selection_sort....."<<endl;
	for(i=0;i<length;i++)
	{
		int Min=i;
		for(j=i;j<length-1;j++)
			if(a[j]<a[Min])
              Min=j;
		swap(a[i],a[Min]);

	}
}
void main()
{
	int a[100],n,i;
	//clock_t start,end;
	cout<<"please input the nummber of the array:"<<endl;
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	cout<<endl;
	show(a,n);
	Insert_Sort(a,n);
	show(a,n);
	Bubble_Sort(a,n);
	show(a,n);
	Selection_Sort(a,n);
	show(a,n);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值