forked from YingjunHu/Allen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajority_count.c
More file actions
68 lines (62 loc) · 1.86 KB
/
majority_count.c
File metadata and controls
68 lines (62 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* ********************************
* CSE 30 - HW 4
* ********************************/
/* int count(int *arr, int len, int target)
* Args:
* int *arr: address of element 0 of an integer array
* int len: length of "arr"
* int target: the value for which we are returning
* the number of times it occurs in "arr"
* Return:
* int: the number of times "target" appears in
* "arr"
*/
int count(int *arr, int len, int target) {
int i;
int ret_count = 0;
for(i = 0; i < len; ++i) {
if(arr[i] == target) {
++ret_count;
}
}
return ret_count;
}
/* int majority_count(int * arr, int len, int * result)
* Args:
* int * arr: address of element 0 of an integer array
* int len: the length of "arr"
* int * result: if not NULL, the location at which the
* value of the majority element should be stored.
* Return:
* int: the count of the majority element in the
* array. 0 if no majority element. An array
* of length 0 has no majority.
*/
int majority_count(int * arr, int len, int * result) {
if(len == 0) {
return 0;
}
if(len == 1) {
if(result) {*result = arr[0];}
return 1;
}
int left_majority, right_majority, c;
int left_majority_count = majority_count(arr, len/2, &left_majority);
int right_majority_count = majority_count(arr+len/2, len-len/2,
&right_majority);
if(left_majority_count) {
c = count(arr, len, left_majority);
if(c > len/2) {
if(result) {*result = left_majority;}
return c;
}
}
if(right_majority_count) {
c = count(arr, len, right_majority);
if(c > len/2) {
if(result) {*result = right_majority;}
return c;
}
}
return 0;
}