hypre/utilities/binsearch.c

46 lines
1.1 KiB
C

/*BHEADER**********************************************************************
* (c) 1998 The Regents of the University of California
*
* See the file COPYRIGHT_and_DISCLAIMER for a complete copyright
* notice, contact person, and disclaimer.
*
* $Revision$
*********************************************************************EHEADER*/
#include "utilities.h"
/*--------------------------------------------------------------------------
* hypre_BinarySearch
* performs a binary search for value on array list where list needs
* to contain ordered nonnegative numbers
* the routine returns the location of the value or -1
*--------------------------------------------------------------------------*/
int hypre_BinarySearch(int *list, int value, int list_length)
{
int low, high, m;
int not_found = 1;
low = 0;
high = list_length-1;
while (not_found && low <= high)
{
m = (low + high) / 2;
if (value < list[m])
{
high = m - 1;
}
else if (value > list[m])
{
low = m + 1;
}
else
{
not_found = 0;
return m;
}
}
return -1;
}