根據題意可得程式碼:
#include<stdio.h>
#define N 10
int main()
{
int a[N], i, j, k;
//輸入
for(i = 0; i < N; ++i){
scanf ("%d", &a[i]);
}
//選擇排序
for (i = 0; i < N-1; ++i){
k = i;
for (j = i+1; j < N; ++j){
if (a[k] > a[j]){
k = j;
if (k != i){
j = a[k];
a[k] = a[i];
a[i] = j;
//輸出
for (i = 0; i < N; ++i){
printf ("%d ", a[i]);
printf ("\n");
return 0;
根據題意可得程式碼:
#include<stdio.h>
#define N 10
int main()
{
int a[N], i, j, k;
//輸入
for(i = 0; i < N; ++i){
scanf ("%d", &a[i]);
}
//選擇排序
for (i = 0; i < N-1; ++i){
k = i;
for (j = i+1; j < N; ++j){
if (a[k] > a[j]){
k = j;
}
}
if (k != i){
j = a[k];
a[k] = a[i];
a[i] = j;
}
}
//輸出
for (i = 0; i < N; ++i){
printf ("%d ", a[i]);
}
printf ("\n");
return 0;
}