問題:
Description
用選擇法對10個整數從小到大排序。
Input
10個整數
Output
排序好的10個整數
Sample Input
4 85 3 234 45 345 345 122 30 12
Sample Output
3
4
12
30
45
85
122
234
345
345
HINT
提示:可以使用其他排序算法
Source
選擇排序思路:第一次從第一個數開始找到最小的,和第一個數交換,然後從第二個數在找,找到最小的,和第二個交換,依此類推,直至最後一個數。代碼:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10];
int i,j;
int m,t;
for(i=0; i<10; i++)
scanf("%d",&a[i]);
for(i=0; i<10; i++)
{
m=i;
for(j=i; j<10; j++)
{
if(a[j]<a[m])
m=j;
}
if(m!=i)
{
t=a[i];
a[i]=a[m];
a[m]=t;
}
}
for(i=0; i<10; i++)
printf("%d\n",a[i]);
return 0;
}
小結:學習了選擇排序。