-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapprox-sort.c
102 lines (75 loc) · 2.05 KB
/
approx-sort.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "softheap.h"
/* --------------- Metrics ------------------ */
double metric_mispositions(int *output, int n) {
int mispos = 0;
for(int i = 0; i < n; i++) {
if(output[i] != i)
mispos++;
}
return mispos/(double)n;
}
long metric_distance(int *output, int n) {
long dist = 0;
for(int i = 0; i < n; i++) {
dist += (output[i] > i ? output[i] - i : i - output[i]);
}
return dist;
}
//Kendall tau distance. Equivalent to number of pairs in wrong order.
double metric_kendall(int *output, int n) {
long count = 0;
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(output[i] > output[j])
count++;
}
}
return 2*count / (n*(n-1.0));
}
double metric_mispositions_threshold(int *output, int n, int threshold) {
int mispos = 0;
for(int i = 0; i < n; i++) {
if(output[i] > i + threshold || output[i] < i - threshold)
mispos++;
}
return mispos/(double)n;
}
/* --------------- Test -------------------- */
void all_metrics_per_epsilon(int *elts, int n) {
int output[n];
for(int k = 1; k < n; k *= 2) {
double epsilon = ((double)k)/n;
int r = ceil(-log(epsilon)/log(2)) + 5;
softheap *P = makeheap_empty(epsilon);
for(int i = 0; i < n; i++)
insert(P, elts[i]);
for(int i = 0; i < n; i++)
output[i] = extract_min(P);
destroy_heap(P);
printf("r=%d \t\t %f \t\t %ld \t\t %f \n", r, metric_mispositions(output,n),metric_distance(output,n),metric_mispositions_threshold(output,n,n/100));
}
}
//Uniform in [0,k). Not perfect, but should be ok...
int randint(int k) {
return (int)(rand()/(RAND_MAX + 1.0) * k);
}
//Fisher-Yates uniformly random permutation
void random_permutation(int *elts, int n) {
for(int i = 0; i < n; i++) {
int j = randint(i+1);
elts[i] = elts[j];
elts[j] = i;
}
}
int main(int argc, char *argv[]) {
int n = 1000000;
int elts[n];
srand(time(NULL));
random_permutation(elts, n);
all_metrics_per_epsilon(elts, n);
return 0;
}