-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverhalfnum.c
executable file
·82 lines (72 loc) · 2.01 KB
/
overhalfnum.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
int partation(int* num,int start, int end){
//each time find the last num or other ways
int x = num[end];
int i = start;
int j = i - 1;
int tmp;
for(;i<end;i++){
if(num[i] <= x){
++j;
tmp = num[i];
num[i] = num[j];
num[j] = tmp;
}
}
//exchange j+1 with the end
tmp = num[end];
num[end] = num[j+1];
num[j+1] = tmp;
return j+1;
}
int more_than_half(int* num, int len){
int mid = len >> 1;
int end = len - 1;
int start = 0;
//use the quick sort partaion way to find the mid num
int index = partation(num,start,end);
int result;
while(index != mid){
if(index < mid){
start = index + 1;
index = partation(num,start,end);
}
if(index > mid){
end = index - 1;
index = partation(num, start,end);
}
}
result = num[mid];
//check for the result
return result;
}
/*
prove this method in pointer offer is wrong
*/
int over_halfarr(int* num, int len){
if(num == NULL || len <= 0)
return -1;
int result = num[0];
int count = 1;
int i = 1;
for(;i<len;i++){
if(count == 0){
result = num[i];
count = 1;
}else if(result == num[i])
++count;
else
--count;
}
//check for
//TODO : check(num,len,result);
return result;
}
int main(){
int test[9] = {1,2,3,2,2,3,5,4,4};
int result = over_halfarr(test,9);
int result2 = more_than_half(test,9);
printf("%d\n",result);
printf("%d\n",result2);
return 0;
}