Binary search on array
Utilisateur anonyme
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package checkpoint; /** * * @author Omar */ public class BinarySearch { public static int search(int[] a, int first, int last, int key) { int result = 0; if(first > last) { System.out.println("The " + key + " is not found inside the array"); result = -1; } else { int mid = (first + last) / 2; if(key == a[mid]) { result = mid; } if(key a[mid]) { result = search(a, mid + 1, last, key); } } return result; } }