Select Sort & Bubble Sort

来源:互联网 发布:windows dock软件 编辑:程序博客网 时间:2024/06/06 05:52
// Select_sort.c.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include "iostream"
using namespace std;
void select_sort(int a[],int n)
{
int temp;
for(int i=0;i<n-1;++i){
int j=i;
for(int k=i+1;k<n;++k)
if(a[k]<a[j])j=k;
if(j!=i){temp=a[j];a[j]=a[i];a[i]=temp;}
}
}




void bubble_sort(int a[],int n)


{
bool change=true;
for(int i=n-1;i>1&&change;--i){
change=false;
for(int j=0;j<i;++j)
{
if(a[j]>a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
change=true;
}


}
}




}






int _tmain(int argc, _TCHAR* argv[])
{

int b[]={10,2,2,15,9,88,7};
//select_sort(b,7);
//for(int out=0;out<7;++out)
// cout<<b[out]<<endl;
//system("pause");






bubble_sort(b,7);
for(int out=0;out<7;++out)
cout<<b[out]<<endl;
system("pause");


return 0;
}


0 0
原创粉丝点击