Here you can find the source of sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)
public static int sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)
//package com.java2s; /**//from w w w . ja v a2 s . co m * com.mckoi.util.SortUtil 29 Mar 1998 * * Mckoi SQL Database ( http://www.mckoi.com/database ) * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * Version 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License Version 2 for more details. * * You should have received a copy of the GNU General Public License * Version 2 along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Change Log: * * */ public class Main { /** * Quickly finds the index of the given object in the list. If the object * can not be found, it returns the point where the element should be * added. */ public static int sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher) { if (lower >= higher) { if (val.compareTo(list[lower]) > 0) { return lower + 1; } else { return lower; } } int mid = (lower + higher) / 2; Comparable mid_val = list[mid]; if (val.equals(mid_val)) { return mid; } else if (val.compareTo(mid_val) < 0) { return sortedIndexOf(list, val, lower, mid - 1); } else { return sortedIndexOf(list, val, mid + 1, higher); } } }