代码部分:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int cmp1(const void*a,const void *b);
int cmp2(const void*a,const void *b);
int i, j, t;
int a[10];
printf("input 10 numbers:\n");
for (i = 0; i < 10; i++) scanf("%d", &a[i]);
printf("\n");
for (j = 10 - 1; j > 0; j--)//冒泡排序 由小到大排序
for (i = 0; i < j; i++)
if (a[i] > a[i + 1]) { t = a[i]; a[i] = a[i + 1]; a[i + 1] = t; }
printf("the sorted numbers of BubbleSort:\n");
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
qsort(a,10,sizeof(int),cmp1);
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
qsort(a,10,sizeof(int),cmp2);
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
int cmp1(const void*a,const void *b)
//快速排序 由小到大排序
{
return (*(int *) a-*(int *)b);
}
int cmp2(const void*a,const void *b)
//快速排序 由大到小排序
{
return (*(int *) b-*(int *)a);
}
结果:

本文详细介绍了使用C语言实现的冒泡排序、快速排序算法,包括从小到大和从大到小的排序过程。通过具体代码示例,展示了如何输入一组数字并进行排序,以及如何使用qsort函数配合自定义比较函数进行高效排序。
1068

被折叠的 条评论
为什么被折叠?



