http://acm.hit.edu.cn/hoj/problem/view?id=1863
顺便复习冒泡和选择
#include<stdio.h>
#include<stdlib.h>
int compar(const void *a, const void *b);
void select_sort(int a[], int n);
void bubble_sort(int a[], int n);
int main()
{
int n, i, a[10];
scanf("%d", &n);
while (n--)
{
for (i = 0; i < 10; i++)
scanf("%d", &a[i]);
qsort(a, 10, sizeof(a[0]), compar);
for (i = 0; i < 9; i++)
printf ("%d ", a[i]);
printf ("%d\n", a[9]);
}
return 0;
}
int compar(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
void bubble_sort(int a[], int n)
{
int i, j ,tmp;
for (i = n; i > 0; i--)
{
for (j = 0; j < i-1; j++)
{
if (a[j] > a[j+1])
{
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
void select_sort(int a[], int n)
{
int i, j, min, tmp;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i + 1; j < n; j++)
if (a[min] > a[j])
min = j;
if (min != i)
{
tmp = a[min];
a[min] = a[i];
a[i] = tmp;
}
}
}