Java examples for java.util:List Search
Perform a binary search over a sorted list for the given key.
/*/*from www. ja v a 2 s . c o m*/ * Copyright (c) 2002-2015, JIDE Software Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ //package com.java2s; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { List a = java.util.Arrays.asList("asdf", "java2s.com"); Object key = "java2s.com"; System.out.println(binarySearch(a, key)); } /** * Perform a binary search over a sorted list for the given key. * * @param a the array to search * @param key the key to search for * @return the index of the given key if it exists in the list, otherwise -1 times the index value at the insertion * point that would be used if the key were added to the list. */ @SuppressWarnings("unchecked") public static int binarySearch(List<Object> a, Object key) { int x1 = 0; int x2 = a.size(); int i = x2 / 2, c; while (x1 < x2) { if (!(a.get(i) instanceof Comparable)) { return i; } c = ((Comparable) a.get(i)).compareTo(key); if (c == 0) { return i; } else if (c < 0) { x1 = i + 1; } else { x2 = i; } i = x1 + (x2 - x1) / 2; } return -1 * i; } /** * Perform a binary search over a sorted array for the given key. * * @param a the array to search * @param key the key to search for * @return the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion * point that would be used if the key were added to the array. */ @SuppressWarnings("unchecked") public static int binarySearch(Object[] a, Object key) { int x1 = 0; int x2 = a.length; int i = x2 / 2, c; while (x1 < x2) { if (!(a[i] instanceof Comparable)) { return i; } c = ((Comparable) a[i]).compareTo(key); if (c == 0) { return i; } else if (c < 0) { x1 = i + 1; } else { x2 = i; } i = x1 + (x2 - x1) / 2; } return -1 * i; } /** * Perform a binary search over a sorted array for the given key. * * @param a the array to search * @param key the key to search for * @return the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion * point that would be used if the key were added to the array. */ public static int binarySearch(int[] a, int key) { return binarySearch(a, key, 0, a.length); } /** * Perform a binary search over a sorted array for the given key. * * @param a the array to search * @param key the key to search for * @param start the start index to search inclusive * @param end the end index to search exclusive * @return the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion * point that would be used if the key were added to the array. */ public static int binarySearch(int[] a, int key, int start, int end) { int x1 = start; int x2 = end; int i = x2 / 2; while (x1 < x2) { if (a[i] == key) { return i; } else if (a[i] < key) { x1 = i + 1; } else { x2 = i; } i = x1 + (x2 - x1) / 2; } return -1 * i; } }