回调函数法 冒泡排序(C语言)
#include <stdio.h>
int small_big(int x, int y)
{
return (x > y) ? 1 : 0;
}
int big_small(int x, int y)
{
return (x < y) ? 1 : 0;
}
void sort(int cmd[], int len, int (*p)(int, int))
{
int i,j,temp;
for(i = 0; i < len-1; i++){
for(j = 0; j < len-1-i; j++){
if(p(cmd[j], cmd[j+1])){
temp = cmd[j];
cmd[j] = cmd[j+1];
cmd[j+1] = temp;
}
}
}
}
int main()
{
int i;
int size;
int cmd[10];
printf("输入10个数:\n");
for(i = 0; i < 10; i++){
scanf("%d",&cmd[i]);
}
printf("\n");
size = sizeof(cmd) / sizeof(cmd[0]);
sort(cmd, size, big_small);
for(i = 0; i < 10; i++){
printf("%d ",cmd[i]);
}
printf("\n");
return 0;
}