- 1 //This is a quicksort algorithm write by xiuxia,it's like C++ STL
-
2 //But can be used in C
-
3 #include <stdio.h>
-
4 #include <stdlib.h>
-
5 #include <string.h>
-
6 //int cmp(int *a, int *b);
-
7 typedef struct
-
8 {
-
9 int x;
-
10 int y;
-
11 }pointer;
-
12
-
13 int numcmp(char *s1,char *s2);
-
14 void qsort_1(void *v[], int left,int right,int (*comp)(void *,void *))
-
15 {
-
16 int i,last;
-
17 void swap(void *v[],int,int); //
-
18 if(left>=right)
-
19 return ;
-
20 swap(v,left,(left+right)/2); //why swap ??
-
21 last=left;
-
22 for(i=left+1;i<=right;i++)
-
23 {
-
24 if( (*comp)( v[i], v[left] )<0 )
-
25 {
-
26 swap(v,++last,i);
-
27 }
-
28 }
-
29 swap(v,left,last);
-
30 qsort_1(v,left,last-1,comp);
-
31 qsort_1(v,last+1,right,comp);
-
32 }
-
33
-
34 void swap(void *v[],int i,int j)
-
35 {
-
36 void *temp;
-
37 temp=v[i];
-
38 v[i]=v[j];
-
39 v[j]=temp;
-
40 }
-
41
-
42 int numcmp(char *s1,char *s2)
-
43 {
-
44 double v1,v2;
-
45 v1=atof(s1);
-
46 v2=atof(s2);
-
47 if(v1<v2)
-
46 v2=atof(s2);
-
47 if(v1<v2)
-
48 return -1;
-
49 else if(v1>v2)
-
50 return 1;
-
51 else
-
52 return 0;
-
53 }
-
54
-
55 int cmp_2(pointer *a, pointer *b)
-
56 {
-
57 int aa=a->x;
-
58 int bb=b->x;
-
59 if(aa<bb)
-
60 return -1;
-
61 else if(aa>bb)
-
62 return 1;
-
63 else
-
64 return 0;
-
65 }
-
66 int cmp(int *a, int *b)
-
67 {
-
68 int aa= (int)*a;
-
69 int bb=(int)*b;
-
70 if(aa<bb)
-
71 return -1;
-
72 else if(aa>bb)
-
73 return 1;
-
74 else
-
75 return 0;
-
76 }
-
77
-
78
-
79 int main()
-
80 {
-
81 //int A[10]={5,8,23,12,0,3,5};
-
82 int *A[10];
-
83 pointer *B[5];
-
84 int i;
-
85 int n=7;
-
86 for (i=0;i<10;i++)
-
87 A[i]=(int *)malloc(4);
-
88 *A[0]=9;
-
89 *A[1]=8;
-
90 *A[2]=23;
-
91 *A[3]=12;
-
92 for(i=0;i<4;i++)
-
91 *A[3]=12;
-
92 for(i=0;i<4;i++)
-
93 {
-
94 printf("%d\n",*A[i]);
-
95 }
-
96
-
97 qsort_1((void **)&A,0,4-1,(int (*)(void *,void *))cmp);
-
98 for(i=0;i<4;i++)
-
99 {
-
100 printf("%d\n",*A[i]);
-
101 }
-
102
-
103 //about B
-
104
-
105 for (i=0;i<5;i++)
-
106 B[i]=(pointer *)malloc(sizeof(pointer));
-
107
-
108 B[0]->x=7;
-
109 B[0]->y=9;
-
110
-
111 B[1]->x=0;
-
112 B[1]->y=6;
-
113
-
114 B[2]->x=4;
-
115 B[2]->y=2;
-
116
-
117 B[3]->x=3;
-
118 B[3]->y=5;
-
119 qsort_1((void **)B,0,3,(int (*)(void *, void *))cmp_2 );
-
120 printf("-------------------------------------\n");
-
121 for(i=0;i<3;i++)
-
122 {
-
123 printf("%d %d\n",B[i]->x,B[i]->y);
-
124 }
-
125 printf("-------------------------------------\n");
-
126 char *lineptr[4];
-
127
-
128 lineptr[0]=(char *)malloc(sizeof(char)*10);
-
129 strcpy(lineptr[0],"zhang");
-
130 lineptr[1]=(char *)malloc(sizeof(char)*10);
-
131 strcpy(lineptr[1],"xiu");
-
132 lineptr[2]=(char *)malloc(sizeof(char)*10);
-
133 strcpy(lineptr[2],"xia");
-
134
-
135 lineptr[3]=(char *)malloc(sizeof(char)*10);
-
136 strcpy(lineptr[3],"love");
-
137
-
136 strcpy(lineptr[3],"love");
-
137
-
138 lineptr[4]=(char *)malloc(sizeof(char)*10);
-
139 strcpy(lineptr[4],"life");
-
140
-
141 qsort_1((void **)lineptr,0,4,(int (*)(void *, void *))strcmp);
-
142
-
143 for(i=0;i<5;i++)
-
144 printf("%s\n",lineptr[i]);
-
145 return 0;
-
146 }
~
阅读(1621) | 评论(0) | 转发(0) |