commit
90ebd8ad99
@ -1,65 +1,68 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements a Binary Search in Java
|
|
||||||
*
|
*
|
||||||
* @author unknown
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class BinarySearch
|
class BinarySearch
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* This method implements the Binary Search
|
* This method implements the Generic Binary Search
|
||||||
*
|
*
|
||||||
* @param array The array to make the binary search
|
* @param array The array to make the binary search
|
||||||
* @param key The number you are looking for
|
* @param key The number you are looking for
|
||||||
* @param lb The lower bound
|
* @param lb The lower bound
|
||||||
* @param up The upper bound
|
* @param ub The upper bound
|
||||||
* @return the location of the key
|
* @return the location of the key
|
||||||
**/
|
**/
|
||||||
public static int BS(int array[], int key, int lb, int ub)
|
|
||||||
|
public static <T extends Comparable<T>> int BS(T array[], T key, int lb, int ub)
|
||||||
{
|
{
|
||||||
if ( lb > ub)
|
if ( lb > ub)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int mid = (lb + ub) / 2;
|
int mid = (ub+lb)/2;
|
||||||
|
int comp = key.compareTo(array[mid]);
|
||||||
|
|
||||||
if (key < array[mid])
|
if (comp < 0)
|
||||||
return (BS(array, key, lb, mid-1));
|
return (BS(array, key, lb, mid-1));
|
||||||
|
|
||||||
if (key > array[mid])
|
if (comp > 0)
|
||||||
return (BS(array, key, mid + 1, ub));
|
return (BS(array, key, mid + 1, ub));
|
||||||
|
|
||||||
return mid;
|
return mid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Driver Program
|
||||||
/**
|
|
||||||
* This is the main method of Binary Search
|
|
||||||
*
|
|
||||||
* @author Unknown
|
|
||||||
* @param args Command line parameters
|
|
||||||
*/
|
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
Scanner input=new Scanner(System.in);
|
Scanner input=new Scanner(System.in);
|
||||||
int array[]=new int[10] ;
|
|
||||||
int key;
|
|
||||||
|
|
||||||
//Input
|
// For INTEGER Input
|
||||||
System.out.println("Enter an array of 10 numbers : ");
|
Integer[] array = new Integer[10];
|
||||||
|
int key = 5;
|
||||||
|
|
||||||
for (int i = 0; i < 10 ; i++ )
|
for (int i = 0; i < 10 ; i++ )
|
||||||
array[i] = input.nextInt();
|
array[i] = i+1;
|
||||||
|
|
||||||
System.out.println("Enter the number to be searched : ");
|
int index = BS(array, key, 0, 9);
|
||||||
|
|
||||||
key = input.nextInt();
|
|
||||||
|
|
||||||
int index=BS(array, key, 0, 9);
|
|
||||||
|
|
||||||
if (index != -1)
|
if (index != -1)
|
||||||
System.out.println("Number found at index number : " + index);
|
System.out.println("Number " + key + " found at index number : " + index);
|
||||||
|
else
|
||||||
|
System.out.println("Not found");
|
||||||
|
|
||||||
|
|
||||||
|
// For STRING Input
|
||||||
|
String[] array1 = {"a", "b", "c", "d", "e"};
|
||||||
|
String key1 = "d";
|
||||||
|
|
||||||
|
int index1 = BS(array1, key1, 0, array1.length-1);
|
||||||
|
|
||||||
|
if (index1 != -1)
|
||||||
|
System.out.println("String " + key1 + " found at index number : " + index1);
|
||||||
else
|
else
|
||||||
System.out.println("Not found");
|
System.out.println("Not found");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user