Binary search: Fast dichotomic search on sorted arrays - Java Language Basics

Java examples for Language Basics:Array

Description

Binary search: Fast dichotomic search on sorted arrays

Demo Code

public class Main {

    static int Dichotomy(int[] array, int left, int right, int key) {
        if (left > right)
            return -1;

        int m = (left + right) / 2;

        if (array[m] == key) {
            return m;
        } else {//from   ww w  . ja  v  a 2 s. co  m
            if (array[m] < key) return Dichotomy(array, m + 1, right, key);
            else return Dichotomy(array, left, m - 1, key);
        }
    }

    static int DichotomicSearch(int[] array, int key) {
        return Dichotomy(array, 0, array.length - 1, key);
    }

    public static void main(String[] args) {
        int[] v = {11, 6, 19, 112, 45, 67, 76, 80, 95};

        System.out.println("Seeking for element 6: Position " + DichotomicSearch(v, 6));
        System.out.println("Seeking for element 80: Position " + DichotomicSearch(v, 80));
        System.out.println("Seeking for element 33: Position " + DichotomicSearch(v, 33));
    }
}

Result


Related Tutorials