Bin's

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 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  阅读(1134)  评论(1编辑  收藏  举报