//将具有20个数组元素的整型数组的前10个数升序排列,后10个降序排列。
#include <stdio.h>
main()
{
int a[20], trans, smlr, bigr;
for (int i = 0; i < 20; i++)
scanf_s("%d", &a[i]);
for (int old = 0; old < 10; old++)
{
smlr = old;
for (int new = 1 + old; new < 10; new++)
if (a[new] < a[old])
smlr = new;
if (smlr != old)
{
trans = a[old];
a[old] = a[smlr];
a[smlr] = trans;
}
}
for (int old = 10; old < 20; old++)
{
bigr = old;
for (int new = 1 + old; new < 20; new++)
if (a[new] > a[old])
bigr = new;
if (bigr != old)
{
trans = a[old];
a[old] = a[bigr];
a[bigr]=trans;
}
}
for (int i = 0; i < 20; i++)
printf("%d ", a[i]);
}
调试结果:
1
2
3
4
5
6
7
8
9
0
12
13
11
15
10
14
16
17
18
19
0 1 2 3 4 5 6 7 8 9 19 18 12 17 11 15 16 14 13 10
D:\Files\vs\test1\x64\Debug\test1.exe (进程 17308)已退出,代码为 0。
按任意键关闭此窗口. . .