PartMC
2.6.1
src
sort.c
Go to the documentation of this file.
1
/* Copyright (C) 2011 Matthew West
2
* Licensed under the GNU General Public License version 2 or (at your
3
* option) any later version. See the file COPYING for details.
4
*/
5
6
/** \file
7
* \brief Wrapper routines for C qsort.
8
*/
9
10
#include <stdlib.h>
11
12
/** \brief Helper function for integer_sort_c()
13
*/
14
int
pair_compare
(
const
void
*a,
const
void
*b)
15
{
16
int
a_val = *((
int
*)a);
17
int
b_val = *((
int
*)b);
18
19
if
(a_val < b_val) {
20
return
-1;
21
}
else
if
(a_val > b_val) {
22
return
1;
23
}
else
{
24
return
0;
25
}
26
}
27
28
/** \brief Sort the given data array and return the permutation.
29
*
30
* On return the \c data array is sorted and the \c perm array
31
* contains the permutation, so that <tt>new_data[i] =
32
* data[perm[i]]</tt>, where \c data is the original data and \c
33
* new_data is the sorted data.
34
*
35
* \param n The length of \c data and \c perm.
36
* \param data The data array (sorted on return).
37
* \param perm The permutation on return: <tt>new_data[i] =
38
* data[perm[i]]</tt>.
39
*/
40
int
integer_sort_c
(
int
n,
int
*data,
int
*perm)
41
{
42
int
*data_perm;
43
int
i;
44
45
data_perm = (
int
*)malloc(
sizeof
(
int
) * 2 * n);
46
for
(i = 0; i < n; i++) {
47
data_perm[2 * i] = data[i];
48
data_perm[2 * i + 1] = i + 1;
49
}
50
qsort(data_perm, n, 2 *
sizeof
(
int
),
pair_compare
);
51
for
(i = 0; i < n; i++) {
52
data[i] = data_perm[2 * i];
53
perm[i] = data_perm[2 * i + 1];
54
}
55
free(data_perm);
56
}
integer_sort_c
int integer_sort_c(int n, int *data, int *perm)
Sort the given data array and return the permutation.
Definition:
sort.c:40
pair_compare
int pair_compare(const void *a, const void *b)
Helper function for integer_sort_c()
Definition:
sort.c:14
PartMC 2.6.1 documentation generated by
doxygen
1.8.20