-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.c
205 lines (163 loc) · 3.24 KB
/
select.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int randomized_partition(int a[], int p, int r){
//get random value
int i = p + rand()%(r-p+1), j;
int temp;
int pivot;
//swap
temp = a[r];
a[r] = a[i];
a[i] = temp;
//partition
pivot = a[r];
i = p-1;
for(j=p;j<r;j++){
if(a[j] <= pivot){
i++;
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
a[r] = a[i+1];
a[i+1] = pivot;
return i+1;
}
int randomized_select_rev(int a[], int p, int r, int k) {
int q;
int i;
if(p==r)
return a[p];
q = randomized_partition(a, p, r);
i = q-p;
if(k == i)
return a[q];
else if(k < i)
return randomized_select_rev(a, p, q-1, k);
else
return randomized_select_rev(a, q+1, r, k-i-1);
}
int randomized_select(int a[], int n, int k){
int result;
// clock_t begin = clock(), end;
//randomize
srand(time(NULL));
result = randomized_select_rev(a, 0, n-1, k);
// end = clock();
// printf("%lf\n", (double)(end - begin)/CLOCKS_PER_SEC);
return result;
}
int partition_rev(int a[], int n, int median){
int i, j;
int temp;
int pivot;
for(i=0;i<n;i++){
if(a[i] == median)
break;
}
//swap
temp = a[n-1];
a[n-1] = a[i];
a[i] = temp;
//partition
pivot = a[n-1];
i = -1;
for(j=0;j<n-1;j++){
if(a[j] <= pivot){
i++;
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
a[n-1] = a[i+1];
a[i+1] = pivot;
return i+1;
}
void insert_sort(int **temp, int n){
int i, j, k;
int num_groups = (n-1)/5 + 1;
int num = 5;
int key;
for(k=0;k<num_groups;k++){
if(k==num_groups-1)
num = n-5*k;
for(j=1;j<num;j++){
key = temp[k][j];
i = j - 1;
while(i>=0 && temp[k][i]>key){
temp[k][i+1] = temp[k][i];
i--;
}
temp[k][i+1] = key;
}
}
}
int find_median(int **temp, int n){
int i;
int num_groups = (n-1)/5 + 1;
int remain_num = n-5*(num_groups-1);
if(n == 1)
return temp[0][0];
else if(n == 2)
return ((temp[0][0] < temp[0][1]) ? temp[0][0] : temp[0][1]);
insert_sort(temp, n);
//set medians only
for(i=0;i<num_groups;i++){
if(i<num_groups-1)
temp[i/5][i%5] = temp[i][2];
else
temp[i/5][i%5] = temp[i][remain_num/2];
}
return find_median(temp, num_groups);
}
int deterministic_select_rev(int a[], int start, int n, int k, int **temp){
int i;
int median;
int pos;
for(i=0;i<n;i++){
temp[i/5][i%5] = a[start+i];
}
median = find_median(temp, n);
pos = partition_rev(a+start, n, median);
if(pos == k)
return median;
else if(pos > k)
return deterministic_select_rev(a, start, pos, k, temp);
else{
start += (pos + 1);
return deterministic_select_rev(a, start, n-1-pos, k-pos-1, temp);
}
}
int deterministic_select(int a[], int n, int k){
int i;
int *temp[400000];
int result;
clock_t begin, end;
for(i=0;i<(n-1)/5+1;i++)
temp[i] = (int*)malloc(5*sizeof(int));
// begin = clock();
result = deterministic_select_rev(a, 0, n, k, temp);
// end = clock();
// printf("%lf\n", (double)(end - begin)/CLOCKS_PER_SEC);
for(i=0;i<(n-1)/5+1;i++)
free(temp[i]);
return result;
}
int checker(int a[], int n, int k, int ans){
int i;
int smaller = 0;
int ifExists = 0;
for(i=0;i<n;i++){
if(a[i] < ans)
smaller++;
else if (a[i] == ans)
ifExists = 1;
}
if(ifExists && smaller==k)
return 1;
else
return 0;
}