1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2 * $Id: densorder.c,v 0.9
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-2001
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
34 * Gyas ROwers Mature At Cryogenic Speed
41 #include "types/simple.h"
42 #include "gmx_fatal.h"
44 /*Make range-array (Permutation identity) for sorting */
45 void rangeArray(int *ar
,int size
)
53 void pswap(int *v1
, int *v2
)
62 void Swap (real
*v1
, real
*v2
)
72 void insertionSort(real
*arr
, int *perm
, int startndx
, int endndx
, int direction
) {
76 for (i
= startndx
; i
<=endndx
; i
++) {
79 while (j
> startndx
&& arr
[j
- 1] > arr
[j
]) {
80 Swap(&arr
[j
],&arr
[j
-1]);
81 pswap(&perm
[j
],&perm
[j
-1]);
90 for (i
= startndx
; i
<=endndx
; i
++) {
93 while (j
> startndx
&& arr
[j
- 1] < arr
[j
]) {
94 Swap(&arr
[j
],&arr
[j
-1]);
95 pswap(&perm
[j
],&perm
[j
-1]);
104 int BinarySearch (real
*array
, int low
, int high
, real key
,int direction
)
110 /*Iterative implementation*/
115 if(key
<array
[mid
-1]) max
=mid
;
121 else if (direction
<0){
124 if(key
>array
[mid
-1]) max
=mid
;
129 }/*end -ifelse direction*/
134 int start_binsearch(real
*array
, int *perm
, int low
, int high
,
135 real key
, int direction
)
137 insertionSort(array
,perm
,low
,high
,direction
);
138 return BinarySearch(array
,low
,high
,key
,direction
);
141 int LinearSearch (double *array
,int startindx
, int stopindx
,
142 double key
,int *count
, int direction
)
144 /*Iterative implementation - assume elements sorted*/
150 for (i
=startindx
;i
<=stopindx
;i
++){
158 else if(direction
<0 ){
160 for(i
=stopindx
;i
>=startindx
;i
--){
170 gmx_fatal(FARGS
,"Startindex=stopindex=%d\n",startindx
);