Skip to content
Snippets Groups Projects
binarySearch.cpp 773 B
Newer Older
  • Learn to ignore specific revisions
  • Lars Bilke's avatar
    Lars Bilke committed
    /**
    
     * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.net)
     *            Distributed under a Modified BSD License.
     *              See accompanying file LICENSE.txt or
     *              http://www.opengeosys.net/LICENSE.txt
     *
     *
     * \file binarySearch.cpp
     *
     * Created on 2010-09-07 by Thomas Fischer
    
    size_t searchElement (double const& val, size_t beg, size_t end, const std::vector<double>& array)
    {
    	if (beg >= end) return std::numeric_limits<size_t>::max();
    	size_t m ((end+beg)/2);
    
    	if (array[m] - val < 0 && array[m+1] - val > 0) {
    		return m;
    	}
    	if (val < array[m]) {
    		return searchElement (val, beg, m, array);
    	}
    	return searchElement (val, m+1, end, array);
    }