forked from Bibeknam/programming-techniques
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection_algorithm.c
221 lines (201 loc) · 3.4 KB
/
selection_algorithm.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void swap(int* a, int* b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int median_1(int a[]) {
return a[0];
}
int median_2(int a[]) {
return a[1];
}
int median_3(int a[]) {
int temp;
if (a[0] < a[1]) {
swap(&a[0], &a[1]);
}
if (a[0] > a[2]) {
a[0] = a[1];
a[1] = a[2];
} else {
return a[0];
}
if (a[0] > a[1]) {
return a[0];
} else {
return a[1];
}
}
int median_4(int a[]) {
int temp1, temp2;
if (a[0] < a[1]) {
swap(&a[0], &a[1]);
}
if (a[2] > a[3]) {
swap(&a[1], &a[2]);
} else {
temp1 = a[1];
temp2 = a[2];
a[1] = a[3];
a[2] = temp1;
a[3] = temp2;
}
if (a[0] > a[1]) {
a[0] = a[1];
a[1] = a[2];
} else {
a[1] = a[3];
}
if (a[0] > a[1]) {
return a[0];
} else {
return a[1];
}
}
// six comparisions
int median_5(int a[]) {
int temp1, temp2;
if (a[0] < a[1]) {
swap(&a[0], &a[1]);
}
if (a[2] > a[3]) {
swap(&a[1], &a[2]);
} else {
temp1 = a[1];
temp2 = a[2];
a[1] = a[3];
a[2] = temp1;
a[3] = temp2;
}
if (a[0] > a[1]) {
a[0] = a[1];
a[1] = a[3];
a[3] = a[4];
} else {
a[1] = a[2];
a[2] = a[3];
a[3] = a[4];
}
if (a[2] > a[3]) {
swap(&a[1], &a[2]);
} else {
temp1 = a[1];
temp2 = a[2];
a[1] = a[3];
a[2] = temp1;
a[3] = temp2;
}
if (a[0] > a[1]) {
a[0] = a[1];
a[1] = a[2];
} else {
a[1] = a[3];
}
if(a[0] > a[1]) {
return a[0];
} else {
return a[1];
}
}
int median(int a[], int n){
switch(n) {
case 1:
return median_1(a);
break;
case 2:
return median_2(a);
break;
case 3:
return median_3(a);
break;
case 4:
return median_4(a);
break;
case 5:
return median_5(a);
break;
default:
break;
}
return -1;
}
// select method returns the kth smallest element
// median is the n/2 th smallest element
// This function runs in linear time
int select(int k, int a[], int n) {
int **subarrays, *medians, *left, *right;
int i, j, l, m,columns, pivot, len_l = 0, len_r = 0;
if (n == 1) {
return a[0];
}
columns = ceil(n / 5.0);
subarrays = (int**)malloc(sizeof(int*)*columns);
for (i=0;i<columns;i++){
subarrays[i] = (int*)malloc(sizeof(int)*5);
}
medians = (int*) malloc(sizeof(int*) * columns);
for (i = 0, j = 0; i < n; i = i + 5, j++) {
if (j == columns) {
for (l = 0; l < n % 5; l++) {
subarrays[j][l] = a[i + l];
}
} else {
for (l = 0; l < 5; l++) {
subarrays[j][l] = a[i + l];
}
}
}
// calculate local medians
for (i = 0; i < columns; i++) {
if (n % 5 != 0 && i == columns - 1) {
medians[i] = median(subarrays[i], n % 5);
} else {
medians[i] = median(subarrays[i], 5);
}
}
if (columns <= 5) {
pivot = median(medians, columns);
} else {
pivot = select(columns/2, medians, columns);
}
// create right and left array
for (i = 0; i < n; i++) {
if (a[i] > pivot) {
len_r++;
}
if (a[i] < pivot) {
len_l++;
}
}
left = (int*) malloc(sizeof(int*) * len_l);
right = (int*) malloc(sizeof(int*) * len_r);
l = 0, m = 0;
for (i = 0; i < n; i++) {
if (a[i] > pivot){
right[l] = a[i];
l++;
}
if (a[i] < pivot) {
left[m] = a[i];
m++;
}
}
if (len_l == k - 1) {
return a[0];
}
if (len_l > k - 1) {
return select(k, left, len_l);
}
if (len_l < k - 1) {
return select(k - len_l - 1, right, len_r);
}
return -1;
}
int main() {
int array[] = {8,7,12,13,3,4,5,6,19,20,9,10,11,14,15,16,17,18,1,2};
printf("Median is %d\n", select(10, array, 20));
}