对10个数组快速排序&&查找前几大
1 /*
2 *快速排序函数的参数如果写成unsigned int类型,要注意unsigned 0 < int -1
3 */
4 #include<stdio.h>
5 #include<stdlib.h>
6
7 void quickSort(unsigned int * p,int start,int end);
8 int partition(unsigned int *p, int start, int end);
9 void print(unsigned int (*p)[10]);
10 unsigned int (*pGlobal) [10];
11 void findSeveralMax(unsigned int (*p)[10],unsigned int count);
12
13 int main(void)
14 {
15 unsigned int a1[10],a2[10],a3[10],a4[10],a5[10],
16 a6[10],a7[10],a8[10],a9[10],a10[10];
17
18 unsigned int (*p) [10];
19 int i,j;
20 p= a1;
21 pGlobal = a1;
22
23 /*
24 *p[0] = 12;
25 *p[1] = 14;
26 printf("%p,%d,%d\n",p,*p[0],p[1][0]);
27 printf("%p\n",p[1]);
28 p = a2;
29 printf("%p\n",p);
30 */
31
32 //生成随机数,初始化10个数组。
33 for( i= 0;i<10;i++)
34 for( j= 0;j<10;j++)
35 p[i][j] = rand()%100;
36
37 print(p);
38
39 //对10个数组排序 降序
40 for(i=0;i<10;i++)
41 {
42 quickSort(p[i],0,9);
43 }
44
45 print(p);
46
47 //查找前几大。
48 findSeveralMax(pGlobal,10);
49 printf("\n");
50
51 return 1;
52 }
53
54 //输出当前数组情况
55 void print(unsigned int (*p)[10])
56 {
57 int i = 0,j=0;
58 for(;i<10;i++)
59 {
60 for(;j<10;j++)
61 printf("%d\t ",p[i][j]);
62 printf("\n");
63 j = 0;
64 }
65
66 printf("\n\n");
67 }
68
69 //快速排序
70 void quickSort(unsigned int * p,int start,int end)
71 {
72 unsigned int pivotloc;
73 if(start<end)
74 {
75 pivotloc = partition(p,start,end);
76 quickSort(p,start,pivotloc-1);
77 quickSort(p,pivotloc+1,end);
78 }
79
80 }
81
82 //定位
83 int partition(unsigned int *p, int start, int end)
84 {
85 unsigned int key = p[start];
86 while(start<end)
87 {
88 while(start<end && p[end]<=key)
89 end--;
90 p[start] = p[end];
91
92 while(start<end && p[start]>=key)
93 start++;
94 p[end] = p[start];
95
96 }
97
98 //print(pGlobal);
99 p[start] = key;
100 return start;
101 }
102
103 //从10个数组里查找前count大
104 void findSeveralMax(unsigned int (*p)[10],unsigned int count)
105 {
106 unsigned int *max = &p[0][0];
107 unsigned int i = 0,j=0;
108 printf("\n开始查找--------------------\n");
109 while(count!=0)
110 {
111 for(i=0;i<10;i++)
112 {
113 for(j=0;j<10;j++)
114 {
115 if(p[i][j]>*max)
116 {
117 max = &p[i][j];
118 break;
119 }
120 }
121 }
122 printf("%d ",*max);
123 *max = 0;
124 count--;
125 }
126
127 printf("\n结束查找--------------------\n");
128
129 }
2 *快速排序函数的参数如果写成unsigned int类型,要注意unsigned 0 < int -1
3 */
4 #include<stdio.h>
5 #include<stdlib.h>
6
7 void quickSort(unsigned int * p,int start,int end);
8 int partition(unsigned int *p, int start, int end);
9 void print(unsigned int (*p)[10]);
10 unsigned int (*pGlobal) [10];
11 void findSeveralMax(unsigned int (*p)[10],unsigned int count);
12
13 int main(void)
14 {
15 unsigned int a1[10],a2[10],a3[10],a4[10],a5[10],
16 a6[10],a7[10],a8[10],a9[10],a10[10];
17
18 unsigned int (*p) [10];
19 int i,j;
20 p= a1;
21 pGlobal = a1;
22
23 /*
24 *p[0] = 12;
25 *p[1] = 14;
26 printf("%p,%d,%d\n",p,*p[0],p[1][0]);
27 printf("%p\n",p[1]);
28 p = a2;
29 printf("%p\n",p);
30 */
31
32 //生成随机数,初始化10个数组。
33 for( i= 0;i<10;i++)
34 for( j= 0;j<10;j++)
35 p[i][j] = rand()%100;
36
37 print(p);
38
39 //对10个数组排序 降序
40 for(i=0;i<10;i++)
41 {
42 quickSort(p[i],0,9);
43 }
44
45 print(p);
46
47 //查找前几大。
48 findSeveralMax(pGlobal,10);
49 printf("\n");
50
51 return 1;
52 }
53
54 //输出当前数组情况
55 void print(unsigned int (*p)[10])
56 {
57 int i = 0,j=0;
58 for(;i<10;i++)
59 {
60 for(;j<10;j++)
61 printf("%d\t ",p[i][j]);
62 printf("\n");
63 j = 0;
64 }
65
66 printf("\n\n");
67 }
68
69 //快速排序
70 void quickSort(unsigned int * p,int start,int end)
71 {
72 unsigned int pivotloc;
73 if(start<end)
74 {
75 pivotloc = partition(p,start,end);
76 quickSort(p,start,pivotloc-1);
77 quickSort(p,pivotloc+1,end);
78 }
79
80 }
81
82 //定位
83 int partition(unsigned int *p, int start, int end)
84 {
85 unsigned int key = p[start];
86 while(start<end)
87 {
88 while(start<end && p[end]<=key)
89 end--;
90 p[start] = p[end];
91
92 while(start<end && p[start]>=key)
93 start++;
94 p[end] = p[start];
95
96 }
97
98 //print(pGlobal);
99 p[start] = key;
100 return start;
101 }
102
103 //从10个数组里查找前count大
104 void findSeveralMax(unsigned int (*p)[10],unsigned int count)
105 {
106 unsigned int *max = &p[0][0];
107 unsigned int i = 0,j=0;
108 printf("\n开始查找--------------------\n");
109 while(count!=0)
110 {
111 for(i=0;i<10;i++)
112 {
113 for(j=0;j<10;j++)
114 {
115 if(p[i][j]>*max)
116 {
117 max = &p[i][j];
118 break;
119 }
120 }
121 }
122 printf("%d ",*max);
123 *max = 0;
124 count--;
125 }
126
127 printf("\n结束查找--------------------\n");
128
129 }