-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathliblsb_sort.cpp
125 lines (94 loc) · 2.56 KB
/
liblsb_sort.cpp
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
/* Source: GNU Scientific Library: https://www.gnu.org/software/gsl/ */
#include "liblsb_internal.hpp"
#include <stdlib.h>
typedef int (*gsl_comparison_fn_t) (const void *, const void *);
int cmp_dbl (const void *a, const void *b);
void gsl_heapsort (void *data, size_t count, size_t size, gsl_comparison_fn_t compare);
void lsb_sort (double *data, int count){
gsl_heapsort((void *) data, count, sizeof(double), (gsl_comparison_fn_t) & cmp_dbl);
}
static inline void swap (void *base, size_t size, size_t i, size_t j);
static inline void downheap (void *data, const size_t size, const size_t N, size_t k, gsl_comparison_fn_t compare);
/* Inline swap function for moving objects around */
static inline void
swap (void *base, size_t size, size_t i, size_t j)
{
register char *a = size * i + (char *) base;
register char *b = size * j + (char *) base;
register size_t s = size;
if (i == j)
return;
do
{
char tmp = *a;
*a++ = *b;
*b++ = tmp;
}
while (--s > 0);
}
#define CMP(data,size,j,k) (compare((char *)(data) + (size) * (j), (char *)(data) + (size) * (k)))
static inline void
downheap (void *data, const size_t size, const size_t N, size_t k, gsl_comparison_fn_t compare)
{
while (k <= N / 2)
{
size_t j = 2 * k;
if (j < N && CMP (data, size, j, j + 1) < 0)
{
j++;
}
if (CMP (data, size, k, j) < 0)
{
swap (data, size, j, k);
}
else
{
break;
}
k = j;
}
}
void
gsl_heapsort (void *data, size_t count, size_t size, gsl_comparison_fn_t compare)
{
/* Sort the array in ascending order. This is a true inplace
algorithm with N log N operations. Worst case (an already sorted
array) is something like 20% slower */
size_t N;
size_t k;
if (count == 0)
{
return; /* No data to sort */
}
/* We have n_data elements, last element is at 'n_data-1', first at
'0' Set N to the last element number. */
N = count - 1;
k = N / 2;
k++; /* Compensate the first use of 'k--' */
do
{
k--;
downheap (data, size, N, k, compare);
}
while (k > 0);
while (N > 0)
{
/* first swap the elements */
swap (data, size, 0, N);
/* then process the heap */
N--;
downheap (data, size, N, 0, compare);
}
}
int
cmp_dbl (const void *a, const void *b)
{
const double x = *(const double *) a;
const double y = *(const double *) b;
if (x > y)
return 1;
if (x == y)
return 0;
else
return -1;
}