~ubuntu-branches/debian/experimental/eso-midas/experimental

« back to all changes in this revision

Viewing changes to libsrc/math/sort.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2015-03-17 15:17:38 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20150317151738-04qxxeqm36oful9i
Tags: 15.02pl1.1-1~exp1
New upstream version

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
===========================================================================*/
28
28
 
29
29
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
 
.COPYRIGHT   (c)  1995  European Soutern Observatory
 
30
.COPYRIGHT   (c)  1995  European Southern Observatory
31
31
.IDENT       hsort.c
32
32
.LANGUAGE    C
33
33
.AUTHOR      P.Grosbol,  IPG/ESO
34
34
.ENVIRON     UNIX
35
35
.KEYWORDS    sort, heapsort
36
 
.COMMENT     Algorithm is adapted from 'Numerical Recipes in C' p.247
 
36
.COMMENT     Making use of heapsort.c module
37
37
.VERSION     1.0  1995-Mar-09 : Creation,  PJG
 
38
.VERSION     2.0  2015-Feb-09 : Using heapsort module, PBA
38
39
-----------------------------------------------------------------------*/
39
40
 
 
41
#include <stdlib.h>
 
42
#include "mutil.h" 
 
43
 
40
44
void hsort(n, ra)
41
45
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
42
46
.PURPOSE   sort array in place using heapsort
45
49
int        n;                    /* no. of elements in array           */
46
50
float      *ra;                   /* pointer to array to be sorted      */
47
51
{
48
 
  int      l, j, ir, i;
49
 
  float    rra;
50
 
 
51
 
  l = n >> 1;
52
 
  ir = n - 1;
53
 
 
54
 
  while (1) {
55
 
     if (l>0)
56
 
       rra = ra[--l];
57
 
     else {
58
 
        rra = ra[ir];
59
 
        ra[ir] = ra[0];
60
 
        if (--ir == 0) {
61
 
           ra[0] = rra;
62
 
           return;
63
 
         }
64
 
      }
65
 
     i = l;
66
 
     j = (l << 1) + 1;
67
 
     while (j<=ir) {
68
 
        if (j<ir && ra[j]<ra[j+1]) ++j;
69
 
        if (rra<ra[j]) {
70
 
           ra[i] = ra[j];
71
 
           j += (i=j) + 1;
72
 
         }
73
 
        else j = ir + 1;
74
 
      }
75
 
     ra[i] = rra;
76
 
   }
77
 
}
 
52
 
 
53
  int *ia = (int*)malloc((size_t)n*sizeof(int)); 
 
54
  heapFillRank(n,ia,0);   
 
55
  heapSortFloat(n,ra,ia); 
 
56
  free(ia); 
 
57
 
 
58
}
 
59
 
 
60
int compare_doubles (
 
61
   const void *a, 
 
62
   const void *b)
 
63
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
64
.PURPOSE   compares doubles the way strncmp does
 
65
.RETURN    integer value, negative if the first argument is "less" than 
 
66
           the second, zero if they are "equal", and positive if the first 
 
67
           argument is "greater". 
 
68
-----------------------------------------------------------------------*/
 
69
{
 
70
  return (int) (*(double*)a - *(double*)b);
 
71
}
 
72
 
 
73
int compare_floats (
 
74
   const void *a, 
 
75
   const void *b)
 
76
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
77
.PURPOSE   compares doubles the way strncmp does
 
78
.RETURN    integer value, negative if the first argument is "less" than 
 
79
           the second, zero if they are "equal", and positive if the first 
 
80
           argument is "greater". 
 
81
-----------------------------------------------------------------------*/
 
82
{
 
83
  return (int) (*(float*)a - *(float*)b);
 
84
}
 
85
 
 
86
 
 
87
int compare_ints (
 
88
   const void *a, 
 
89
   const void *b)
 
90
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
91
.PURPOSE   compares integers the way strncmp does
 
92
.RETURN    integer value, negative if the first argument is "less" than 
 
93
           the second, zero if they are "equal", and positive if the first 
 
94
           argument is "greater". 
 
95
-----------------------------------------------------------------------*/
 
96
{
 
97
  return (int) (*(int*)a - *(int*)b);
 
98
}
 
99
 
 
100
void std_sorti(n, ra)
 
101
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
102
.PURPOSE   sort array in place using quicksort algorithm for stdlib.h
 
103
.RETURN    none
 
104
-----------------------------------------------------------------------*/
 
105
int        n;                    /* no. of elements in array           */
 
106
int        *ra;                   /* pointer to array to be sorted      */
 
107
{
 
108
  qsort(ra, n, sizeof(int), compare_ints); 
 
109
}
 
110
 
 
111
void std_sortf(n, ra)
 
112
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
113
.PURPOSE   sort array in place using quicksort algorithm for stdlib.h
 
114
.RETURN    none
 
115
-----------------------------------------------------------------------*/
 
116
int        n;                    /* no. of elements in array           */
 
117
float      *ra;                   /* pointer to array to be sorted      */
 
118
{
 
119
  qsort(ra, n, sizeof(float), compare_floats); 
 
120
}
 
121
 
 
122
void std_sortd(n, ra)
 
123
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
124
.PURPOSE   sort array in place using quicksort algorithm for stdlib.h
 
125
.RETURN    none
 
126
-----------------------------------------------------------------------*/
 
127
int        n;                    /* no. of elements in array           */
 
128
double     *ra;                   /* pointer to array to be sorted      */
 
129
{
 
130
  // Calling STDLIB qsort
 
131
  qsort(ra, n, sizeof(double), compare_doubles); 
 
132
}
 
133
 
 
134
void quick_sort(n, ra)
 
135
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
136
.PURPOSE   sort array in place using heapsort
 
137
.RETURN    none
 
138
-----------------------------------------------------------------------*/
 
139
int        n;                    /* no. of elements in array           */
 
140
float      *ra;                  /* pointer to array to be sorted      */
 
141
{
 
142
  std_sortf(n, ra); 
 
143
}
 
144
 
 
145