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"
43 /*Make range-array (Permutation identity) for sorting */
44 void rangeArray(int *ar
,int size
)
52 void pswap(int *v1
, int *v2
)
61 void Swap (real
*v1
, real
*v2
)
71 void insertionSort(real
*arr
, int *perm
, int startndx
, int endndx
, int direction
) {
75 for (i
= startndx
; i
<=endndx
; i
++) {
78 while (j
> startndx
&& arr
[j
- 1] > arr
[j
]) {
79 Swap(&arr
[j
],&arr
[j
-1]);
80 pswap(&perm
[j
],&perm
[j
-1]);
89 for (i
= startndx
; i
<=endndx
; i
++) {
92 while (j
> startndx
&& arr
[j
- 1] < arr
[j
]) {
93 Swap(&arr
[j
],&arr
[j
-1]);
94 pswap(&perm
[j
],&perm
[j
-1]);
103 int BinarySearch (real
*array
, int low
, int high
, real key
,int direction
)
109 /*Iterative implementation*/
114 if(key
<array
[mid
-1]) max
=mid
;
120 else if (direction
<0){
123 if(key
>array
[mid
-1]) max
=mid
;
128 }/*end -ifelse direction*/
132 int start_binsearch(real
*array
, int *perm
, int low
, int high
,
133 real key
, int direction
)
135 insertionSort(array
,perm
,low
,high
,direction
);
136 return BinarySearch(array
,low
,high
,key
,direction
);
139 int LinearSearch (double *array
,int startindx
, int stopindx
,
140 double key
,int *count
, int direction
)
142 /*Iterative implementation - assume elements sorted*/
148 for (i
=startindx
;i
<=stopindx
;i
++){
156 else if(direction
<0 ){
158 for(i
=stopindx
;i
>=startindx
;i
--){
168 fprintf(stderr
,"Error: startindex=stopindex=%d\n",startindx
);