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)
48 for (i = 0; i < size; i++)
54 void pswap(int *v1, int *v2)
63 void Swap (real *v1, real *v2)
73 void insertionSort(real *arr, int *perm, int startndx, int endndx, int direction)
79 for (i = startndx; i <= endndx; i++)
83 while (j > startndx && arr[j - 1] > arr[j])
85 Swap(&arr[j], &arr[j-1]);
86 pswap(&perm[j], &perm[j-1]);
96 for (i = startndx; i <= endndx; i++)
100 while (j > startndx && arr[j - 1] < arr[j])
102 Swap(&arr[j], &arr[j-1]);
103 pswap(&perm[j], &perm[j-1]);
112 int BinarySearch (real *array, int low, int high, real key, int direction)
118 /*Iterative implementation*/
125 if (key < array[mid-1])
137 else if (direction < 0)
142 if (key > array[mid-1])
153 } /*end -ifelse direction*/
158 int start_binsearch(real *array, int *perm, int low, int high,
159 real key, int direction)
161 insertionSort(array, perm, low, high, direction);
162 return BinarySearch(array, low, high, key, direction);
165 int LinearSearch (double *array, int startindx, int stopindx,
166 double key, int *count, int direction)
168 /*Iterative implementation - assume elements sorted*/
174 keyindex = startindx;
175 for (i = startindx; i <= stopindx; i++)
185 else if (direction < 0)
188 for (i = stopindx; i >= startindx; i--)
201 gmx_fatal(FARGS, "Startindex=stopindex=%d\n", startindx);