Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
IndxReal.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file IndxReal.c
5 
6  \version V1.4
7  \date 07.07.14
8  \brief Index heapsort a REAL array
9 
10  \copyright (c) UCL / Dr. Andrew C. R. Martin 1991-2014
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39  This routine uses a heapsort to index a floating point array
40  such that arrin[indx[j]] is in ascending order with j.
41  It is modified from the FORTRAN version in 'Numerical Recipes'
42  Page 233. This version correctly sorts from array element 0
43  as opposed to 1 in the FORTRAN version.
44 
45 **************************************************************************
46 
47  Usage:
48  ======
49 
50 
51  IndexReal(arrin,indx,n);
52  Input: int n Number of elements in array
53  REAL *arrin Array to be indexed
54  Output: int *indx Index array
55 
56 
57 **************************************************************************
58 
59  Revision History:
60  =================
61 - V1.0 23.06.90 Original
62 - V1.1 01.06.92 ANSIed and autodoc'd
63 - V1.2 19.07.93 Corrected bug (said j=l+1 rather than j=l+l). Oops!
64 - V1.3 08.07.96 Changed from double to REAL and tidied.
65 - V1.4 07.07.14 Use bl prefix for functions By: CTP
66 
67 *************************************************************************/
68 /* Doxygen
69  -------
70  #GROUP General Programming
71  #SUBGROUP Sorting
72  #FUNCTION blIndexReal()
73  Index an array by Heapsort.
74 */
75 /************************************************************************/
76 /* Includes
77 */
78 #include <math.h>
79 #include "MathType.h"
80 
81 /************************************************************************/
82 /* Defines and macros
83 */
84 
85 /************************************************************************/
86 /* Globals
87 */
88 
89 /************************************************************************/
90 /* Prototypes
91 */
92 
93 /************************************************************************/
94 /*>void blIndexReal(REAL *arrin, int *indx, int n)
95  -----------------------------------------------
96 *//**
97 
98  \param[in] *arrin Array to be indexed
99  \param[in] n Number of elements in array
100  \param[out] *indx Index array
101 
102  Index an array by Heapsort.
103 
104 - 03.06.90 Original
105 - 01.06.92 ANSIed and autodoc'd
106 - 19.07.93 Corrected bug (said j=l+1 rather than j=l+l). Oops!
107 - 08.07.96 Changed from double to REAL and tidied. Changed param order
108 - 07.07.14 Use bl prefix for functions By: CTP
109 */
110 void blIndexReal(REAL *arrin, int *indx, int n)
111 {
112  int i, j, l,
113  ir,
114  indxt;
115  REAL q;
116 
117  for(j=0; j<n; j++)
118  indx[j]=j;
119 
120  l = n/2+1;
121  ir = n;
122 
123  for(;;)
124  {
125  if(l>1)
126  {
127  indxt = indx[--l - 1];
128  q = arrin[indxt];
129  }
130  else
131  {
132  indxt = indx[ir-1];
133  q = arrin[indxt];
134  indx[ir-1] = indx[0];
135  if(--ir == 1)
136  {
137  indx[0] = indxt;
138  return;
139  }
140  }
141  i = l;
142  j = l+l;
143 
144  while(j <= ir)
145  {
146  if(j < ir)
147  {
148  if(arrin[indx[j-1]] < arrin[indx[j]])
149  j++;
150  }
151 
152  if(q < arrin[indx[j-1]])
153  {
154  indx[i-1] = indx[j-1];
155  i = j;
156  j += j;
157  }
158  else
159  {
160  j = ir+1;
161  }
162  }
163  indx[i-1] = indxt;
164  }
165 }
166 
double REAL
Definition: MathType.h:67
void blIndexReal(REAL *arrin, int *indx, int n)
Definition: IndxReal.c:110
Type definitions for maths.