-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmomAlgo.cpp
More file actions
46 lines (40 loc) · 1.16 KB
/
Copy pathmomAlgo.cpp
File metadata and controls
46 lines (40 loc) · 1.16 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
int find(int[] a, int s, int n, int k ) {
// start point s, length n, find k-th number
if ( n == 1 && k == 1 )
return a[s];
int m = (n+4) /5;
int[] mid = new int[m];
for (int i=0; i<m; i++) {
int t = s+i*5; // 5-elements block pointer
if ( n-t > 4 ) {
sort(a, t, 5); // sort 5 elements
mid[i] = a[t+2];
}
else { // less than 5 left
sort(a, t, n-t); // sort the rest
mid[i] = a[t+(n-t-1)/2];
}
}
int pivot = find(mid, 0, m, (m+1)/2);
for (int i=0; i<n; i++) { // find pivot location
if (a[s+i] == pivot ) {
swap(a, s+i, s+n-1);
break;
}
}
int pos = 0;
for (int i=0; i<n-1; i++) { // using pivot to part
if ( a[s+i] < pivot ) {
if ( i != pos )
swap(a, s+i, s+pos);
pos++;
}
}
swap(a, s+pos, s+n-1);
if ( pos == k-1 )
return pivot;
else if ( pos > k-1 )
return find(a, s, pos, k);
else
return find(a, s+pos+1, n-pos-1, k-pos-1);
}