Java Array Sort sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)

Here you can find the source of sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)

Description

Quickly finds the index of the given object in the list.

License

Open Source License

Declaration

public static int sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher) 

Method Source Code

//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);
        }

    }
}

Related

  1. SortDoubleDimensionArray(String StrArr[][])
  2. sorted(int[] array)
  3. sortedArraysEqual(Object[] arr1, Object[] arr2)
  4. sortedCopyOf(final int[] array)
  5. sortedIndex(double[] values)
  6. sortedMerge(int[] a, int[] b)
  7. sortedMerge(int[] aIds, double[] aVals, int[] bIds, double[] bVals, int[] resIds, double[] resVals)
  8. sortedPointersInto(double d[])
  9. sortedPointersInto_tryingToImproveSpeed(final double d[])