-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorts.h
46 lines (35 loc) · 1.24 KB
/
sorts.h
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
// #############################################################################
// Provides quicksortMap(...), which uses quicksort to return a permutation map
// corresponding to an ordered input array
// #############################################################################
#pragma once
namespace sorts{
// Anonymous namespace creates function only accessible to the sorts namespace
namespace {
int partitionMap(double *arr, int *map, int lo, int hi) {
int tmp;
double pivot = arr[map[hi]];
int i = lo - 1;
for (int j=lo; j<hi; j++) {
if (arr[map[j]] <= pivot) {
i++;
tmp = map[i];
map[i] = map[j];
map[j] = tmp;
}
}
i++;
tmp = map[i];
map[i] = map[hi];
map[hi] = tmp;
return i;
}
}
void quicksortMap(double *arr, int *map, int lo, int hi)
{
if ((lo >= hi) || (lo < 0) ) return;
int p = partitionMap(arr, map, lo, hi);
quicksortMap(arr, map, lo, p-1);
quicksortMap(arr, map, p+1, hi);
}
}