您的位置:首页 > 其它

选择排序

2016-07-14 14:00 141 查看
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void ChoiceSort(int *a,int n)
{
for(int i=0; i<n; i++)
{
int min=a[i],temp=0,t=0;
for(int j=i; j<n; j++)
{
if(a[j]<min)
{
min=a[j];
temp=j;
t=1;
}
}
if(t)
swap(a[i],a[temp]);
}
}
void print(int *a,int n)
{
for(int i=0; i<n; i++)
cout<<*(a+i)<<" ";
cout<<endl;
}
int main()
{
int a[10]= {15,11,14,19,16,13,17,18,12,10};
print(a,10);
ChoiceSort(a,10);
print(a,10);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: