C,使用函数指针的冒泡排序

 1 #include <stdio.h>
2
3 void swap(int *ele1, int *ele2)
4 {
5 int temp = *ele1;
6 *ele1 = *ele2 ;
7 *ele2 = temp ;
8 }
9 void bubbleSort(int *array, int size, int (*func)(int a, int b))
10 {
11
12 int j,m;
13 for (m=1;m<size;m++)
14 {
15 for (j=0;j<size-m;j++)
16 {
17 if ((*func)(array[j],array[j+1]))
18 {
19 swap(&array[j],&array[j+1]);
20 }
21 }
22 }
23 }
24
25 int asc(const int a ,const int b)
26 {
27 return a>b;
28 }
29
30 int desc(const int a,const int b)
31 {
32 return a<b;
33 }
34
35 int main(void)
36 {
37 int array[] = {2,76,3,74,1,44,87,3,6,4,9,10,95,32,14,84,11,6,11};
38 int SIZE = sizeof(array)/sizeof(int);
39 int i=0;
40 int *ptrarr = array;
41 printf ("print the example array:\n");
42 for (;i<SIZE;i++)
43 {
44 printf ("%d ",*ptrarr);
45 ++ptrarr;
46 }
47 printf ("\nEnter for kind of sort function:\n(1 for ASC,2 for DESC):");
48 int option=0;
49 scanf("%d",&option);
50
51 if (option == 1) bubbleSort(array,SIZE,asc);
52 if (option == 2) bubbleSort(array,SIZE,desc);
53 if (option == 0) return 1;
54
55 printf ("print the sorted array:\n");
56 int k=0;
57 for (;k<SIZE;k++)
58 {
59 printf ("%d ",array[k]);
60 }
61 printf("\n***************************\n");
62 }
posted on 2012-04-02 19:21 Jesuca 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Jesuca/archive/2012/04/02/2430224.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值