Binary search works for sorted Arrays so Arrays.sort method is used before using binarySearch method.
Any type that implements Comparable interface is accepted by the generic iterative search method.
Built-in String, Integer and Double classes in Java implement Comparable interface so search method accepts these parameters.
package basics; import java.util.Arrays; public class GenericIterativeBinarySearch { public static <T extends Comparable<T>> boolean search( T[] items, T item ) { if (item == null) { return false; } int low = 0; int high = items.length - 1; while (low <= high) { int ix = low + (high - low) / 2; if (item.compareTo(items[ix]) < 0) { high = ix - 1; } else if (item.compareTo(items[ix]) > 0) { low = ix + 1; } else { return true; } } return false; } public static void main(String[] args) { Integer[] items = { 22, 55, 66, 11, 32, 56, 67, 89, 95, 10 }; Arrays.sort(items); boolean found = search(items, Integer.valueOf(22) ); System.out.println("Integer Array Contains item 22 = "+found); found = search(items, Integer.valueOf(11) ); System.out.println("Integer Array Contains item 11 = "+found); found = search(items, Integer.valueOf(67) ); System.out.println("Integer Array Contains item 67 = "+found); found = search(items, Integer.valueOf(10) ); System.out.println("Integer Array Contains item 10 = "+found); found = search(items, Integer.valueOf(101) ); System.out.println("Integer Array Contains item 101 = "+found); found = search(items, null ); System.out.println("Integer Array Contains item null = "+found); String[] strItems = { "alk", "abc", "adk", "zyt", "fre", "nhy" }; Arrays.sort(strItems); found = search( strItems, "alk" ); System.out.println("String Array Contains item alk = "+found); found = search( strItems, "nhy" ); System.out.println("String Array Contains item nhy = "+found); found = search( strItems, "zyt" ); System.out.println("String Array Contains item zyt = "+found); found = search( strItems, "zyts" ); System.out.println("String Array Contains item zyts = "+found); found = search( strItems, "null" ); System.out.println("String Array Contains item null = "+found); Double[] dItems = { 11.3, 13.3, 6.0, 9.6, 45.7, 23.2 }; Arrays.sort(dItems); found = search( dItems, 13.3 ); System.out.println("Double Array Contains item 13.3 = "+found); found = search( dItems, 14.3 ); System.out.println("Double Array Contains item 14.3 = "+found); found = search( dItems, 23.0 ); System.out.println("Double Array Contains item 23.0 = "+found); } }
Create a GenericIterativeBinarySearch.java file in your workspace.
When the main method inside the GenericIterativeBinarySearch class executed it is going to print :
Integer Array Contains item 22 = true
Integer Array Contains item 11 = true
Integer Array Contains item 67 = true
Integer Array Contains item 10 = true
Integer Array Contains item 101 = false
Integer Array Contains item null = false
String Array Contains item alk = true
String Array Contains item nhy = true
String Array Contains item zyt = true
String Array Contains item zyts = false
String Array Contains item null = false
Double Array Contains item 13.3 = true
Double Array Contains item 14.3 = false
Double Array Contains item 23.0 = false
No comments:
Post a Comment