| 1 |
/* @(#)sort.c 19.1 (ES0-DMD) 02/25/03 13:55:39 */ |
| 2 |
/*=========================================================================== |
| 3 |
Copyright (C) 1995 European Southern Observatory (ESO) |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public |
| 16 |
License along with this program; if not, write to the Free |
| 17 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 18 |
MA 02139, USA. |
| 19 |
|
| 20 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 21 |
Internet e-mail: [email protected] |
| 22 |
Postal address: European Southern Observatory |
| 23 |
Data Management Division |
| 24 |
Karl-Schwarzschild-Strasse 2 |
| 25 |
D 85748 Garching bei Muenchen |
| 26 |
GERMANY |
| 27 |
===========================================================================*/ |
| 28 |
|
| 29 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 30 |
.COPYRIGHT (c) 1995 European Southern Observatory |
| 31 |
.IDENT hsort.c |
| 32 |
.LANGUAGE C |
| 33 |
.AUTHOR P.Grosbol, IPG/ESO |
| 34 |
.ENVIRON UNIX |
| 35 |
.KEYWORDS sort, heapsort |
| 36 |
.COMMENT Making use of heapsort.c module |
| 37 |
.VERSION 1.0 1995-Mar-09 : Creation, PJG |
| 38 |
.VERSION 2.0 2015-Feb-09 : Using heapsort module, PBA |
| 39 |
-----------------------------------------------------------------------*/ |
| 40 |
|
| 41 |
#include <stdlib.h> |
| 42 |
#include "mutil.h" |
| 43 |
|
| 44 |
void hsort(n, ra) |
| 45 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 46 |
.PURPOSE sort array in place using heapsort |
| 47 |
.RETURN none |
| 48 |
-----------------------------------------------------------------------*/ |
| 49 |
int n; /* no. of elements in array */ |
| 50 |
float *ra; /* pointer to array to be sorted */ |
| 51 |
{ |
| 52 |
|
| 53 |
int *ia = (int*)malloc((size_t)n*sizeof(int)); |
| 54 |
heapSortFloat(n,ra,ia); |
| 55 |
free(ia); |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
int compare_doubles ( |
| 60 |
const void *a, |
| 61 |
const void *b) |
| 62 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 63 |
.PURPOSE compares doubles the way strncmp does |
| 64 |
.RETURN integer value, negative if the first argument is "less" than |
| 65 |
the second, zero if they are "equal", and positive if the first |
| 66 |
argument is "greater". |
| 67 |
-----------------------------------------------------------------------*/ |
| 68 |
{ |
| 69 |
return (int) (*(double*)a - *(double*)b); |
| 70 |
} |
| 71 |
|
| 72 |
int compare_floats ( |
| 73 |
const void *a, |
| 74 |
const void *b) |
| 75 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 76 |
.PURPOSE compares doubles the way strncmp does |
| 77 |
.RETURN integer value, negative if the first argument is "less" than |
| 78 |
the second, zero if they are "equal", and positive if the first |
| 79 |
argument is "greater". |
| 80 |
-----------------------------------------------------------------------*/ |
| 81 |
{ |
| 82 |
return (int) (*(float*)a - *(float*)b); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
int compare_ints ( |
| 87 |
const void *a, |
| 88 |
const void *b) |
| 89 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 90 |
.PURPOSE compares integers the way strncmp does |
| 91 |
.RETURN integer value, negative if the first argument is "less" than |
| 92 |
the second, zero if they are "equal", and positive if the first |
| 93 |
argument is "greater". |
| 94 |
-----------------------------------------------------------------------*/ |
| 95 |
{ |
| 96 |
return (int) (*(int*)a - *(int*)b); |
| 97 |
} |
| 98 |
|
| 99 |
void std_sorti(n, ra) |
| 100 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 101 |
.PURPOSE sort array in place using quicksort algorithm for stdlib.h |
| 102 |
.RETURN none |
| 103 |
-----------------------------------------------------------------------*/ |
| 104 |
int n; /* no. of elements in array */ |
| 105 |
int *ra; /* pointer to array to be sorted */ |
| 106 |
{ |
| 107 |
qsort(ra, n, sizeof(int), compare_ints); |
| 108 |
} |
| 109 |
|
| 110 |
void std_sortf(n, ra) |
| 111 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 112 |
.PURPOSE sort array in place using quicksort algorithm for stdlib.h |
| 113 |
.RETURN none |
| 114 |
-----------------------------------------------------------------------*/ |
| 115 |
int n; /* no. of elements in array */ |
| 116 |
float *ra; /* pointer to array to be sorted */ |
| 117 |
{ |
| 118 |
qsort(ra, n, sizeof(float), compare_floats); |
| 119 |
} |
| 120 |
|
| 121 |
void std_sortd(n, ra) |
| 122 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 123 |
.PURPOSE sort array in place using quicksort algorithm for stdlib.h |
| 124 |
.RETURN none |
| 125 |
-----------------------------------------------------------------------*/ |
| 126 |
int n; /* no. of elements in array */ |
| 127 |
double *ra; /* pointer to array to be sorted */ |
| 128 |
{ |
| 129 |
/* Calling STDLIB qsort */ |
| 130 |
qsort(ra, n, sizeof(double), compare_doubles); |
| 131 |
} |
| 132 |
|
| 133 |
void quick_sort(n, ra) |
| 134 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 135 |
.PURPOSE sort array in place using heapsort |
| 136 |
.RETURN none |
| 137 |
-----------------------------------------------------------------------*/ |
| 138 |
int n; /* no. of elements in array */ |
| 139 |
float *ra; /* pointer to array to be sorted */ |
| 140 |
{ |
| 141 |
std_sortf(n, ra); |
| 142 |
} |
| 143 |
|
| 144 |
|