寻找多数元素与桶排序

1.寻找多数元素:(元素的个数大于(n/2)向下取整,如n=10,则个数至少为6),此程序需要手动输入元素的个数,以及相关的每个元素

 

 1 /*
2 * =====================================================================================
3 *
4 * Filename: Majority.cpp
5 *
6 * Description: 找出多数元素
7 *
8 * Version: 1.0
9 * Created: 2011/11/4 16:10:41
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: witcxc, witcxc@gmail.com
14 * Company: whu
15 *
16 * =====================================================================================
17 */
18 #include <stdio.h>
19
20 #define MAX 100
21
22 int candidate(int Array[],int member,int limit);
23 int Majority(int Arrray[],int limit);
24 int main(int argc,char **argv)
25 {
26 // int Array[MAX]={1,2,3,4,2,1,2,2,2,2};
27 int tmp=0,max,Array[MAX];
28 printf("Please decide the number of the elements(eg,10):\n");
29 while(scanf("%d",&max)!=1)
30 {
31 printf("Input error!\nInput again!\n");
32 fflush(stdin);
33 }
34 printf("Please input the %d elements:\n",max);
35 while(tmp<max)
36 {
37 printf("Array[%2d]:",tmp+1);
38 if(scanf("%d",&Array[tmp])!=1)
39 {
40 printf("Input error!Input again!\n");
41 continue;
42 }
43 else
44 {
45 tmp++;
46 }
47 }
48 int Major = -1;
49 Major = Majority(Array,max);
50 if(Major==-1)
51 printf("There's no the majority element!\n");
52 else
53 printf("The majority element is %2d !\n",Major);
54 fflush(stdin);
55 getchar();
56 return 0;
57 }
58
59 int Majority(int Array[],int limit)
60 {
61 int current = candidate(Array,0,limit);
62 int count = 0,tmp = 0;
63 for ( tmp = 0;tmp < limit; tmp++)
64 {
65 if ( Array[tmp]==current )
66 count++;
67 }
68 if ( count > limit/2 )
69 {
70 return current;
71 }
72 else
73 return -1;
74 }
75
76 int candidate(int Array[],int member,int limit)
77 {
78 int tmp = member,current = Array[member],count = 1;
79 while(tmp<limit-1&&count>0)
80 {
81 tmp++;
82 if ( Array[tmp]==current )
83 count++;
84 else
85 count--;
86 }
87 if(tmp == limit-1)
88 return current;
89 else
90 return candidate(Array,tmp+1,limit);
91 }

 运行结果:

2.桶排序,此程序需要手动输入元素的个数,以及相关的每个元素,每个相关的元素值必须小于10,输出排序的结果,程序结束。

 1 /*
2 * =====================================================================================
3 *
4 * Filename: bucket.cpp
5 *
6 * Description: 桶排序
7 *
8 * Version: 1.0
9 * Created: 2011/11/4 16:10:41
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: witcxc, witcxc@gmail.com
14 * Company: whu
15 *
16 * =====================================================================================
17 */
18 #include <stdio.h>
19 #define MAX 100
20
21 void BucketSorter(int array[], int n, int max)
22 {
23 int *TempArray=new int[n];
24 int *count=new int[max];
25 int i;
26 for (i=0;i<n;i++)
27 {
28 TempArray[i]=array[i];
29 }
30
31 for(i=0;i<max;i++)
32 count[i]=0;
33
34 for (i=0;i<n;i++)
35 count[array[i]]++;
36
37 for (i=1;i<max;i++)
38 count[i]=count[i-1]+count[i];
39
40
41 for(i=n-1;i>=0;i--)
42 array[--count[TempArray[i]]]=TempArray[i];
43
44 }
45
46 int main(int argc, char* argv[])
47 {
48 int tmp=0,max,array[MAX];
49 printf("Please decide the number of the elements(eg,10):\n");
50 while(scanf("%d",&max)!=1)
51 {
52 printf("Input error!\nInput again!\n");
53 fflush(stdin);
54 }
55 printf("Please input the %d elements(must be smaller than 10):\n",max);
56 while(tmp<max)
57 {
58 printf("Array[%2d]:",tmp+1);
59 if(scanf("%d",&array[tmp])!=1)
60 {
61 printf("Input error!Input again!\n");
62 continue;
63 }
64 else
65 {
66 if(array[tmp]>9)
67 {
68 printf("Error!Must be smaller than 10!\n");
69 continue;
70 }
71 tmp++;
72 }
73 }
74
75 BucketSorter(array,max,10);
76
77
78 printf("The sorted order is :\n");
79 for (int i=0;i<max;++i)
80 {
81 printf("%d ",array[i]);
82 }
83 printf("\n");
84 fflush(stdin);
85 getchar();
86 return 0;
87 }


运行结果:



posted on 2011-11-13 20:02  witcxc  阅读(288)  评论(0编辑  收藏  举报

导航