本题要求实现一个用选择法对整数数组进行简单排序的函数。
函数接口定义:
void sort( int a[], int n );
其中a是待排序的数组,n是数组a中元素的个数。该函数用选择法将数组a中的元素按升序排列,结果仍然在数组a中。
裁判测试程序样例:
void sort( int a[], int n );
int main()
{
int i, n;
int a[MAXN];
scanf("%d", &n);
for( i=0; i<n; i++ )
scanf("%d", &a[i]);
sort(a, n);
printf("After sorted the array is:");
for( i = 0; i < n; i++ )
printf(" %d", a[i]);
printf("\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
4
5 1 7 6
输出样例:
After sorted the array is: 1 5 6 7
下面是代码:
(```)
void sort( int a[], int n )
{
int i,j;
int temp;
int index;
for(i=0;i<n-1;i++)
{
index=i;
for(j=i+1;j<n;j++)
if(a[j]<a[index])
index=j;
temp=a[index];a[index]=a[i];a[i]=temp;
}
}
(```)
原文:https://www.cnblogs.com/wj2587022594/p/12080877.html