题解 | #牛牛的排序#(选择排序算法解题)
牛牛的排序
http://www.nowcoder.com/practice/26a0c92e9266443887a3bf81aff8e188
void exchange(int*a,int*b)
{
int c = *a;
*a = *b;
*b = c;
}
void select_sort(int*array,int n)
{
int i = 0;
int j = 0;
int k = 0;
for(;i<n;++i)
{
k = i;
for(j = i;j<n;++j)
{
if(*(array+j) < *(array+k))
{
k=j;
}
}
exchange(&array[i],&array[k]);
printf("%d ",array[i]);
}
}
int main()
{
int n = 0;
int array[100] = {0};
int i = 0;
scanf("%d",&n);
getchar();
for(;i<n;++i)
{
scanf("%d",&array[i]);
}
select_sort(array,n);
return 0;
}