|
| 1 | +/** |
| 2 | + * Algorithms-in-Java |
| 3 | + * BinarySearch.java |
| 4 | + */ |
| 5 | +package com.deepak.algorithms.Searching; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class for BinarySearch implementation |
| 9 | + * @author Deepak Malik |
| 10 | + */ |
| 11 | +public class BinarySearch { |
| 12 | + |
| 13 | + /** |
| 14 | + * Main method to start the flow of program |
| 15 | + * @param args |
| 16 | + */ |
| 17 | + public static void main(String[] args) { |
| 18 | + int[] sortedValues = {7, 10, 10, 20, 25, 32, 40, 46, 47, 49, 54, 55, 61, 63, 65, 83, 84, 93}; |
| 19 | + int valueToBeSearched = 84; |
| 20 | + System.out.println("******************* BINARY SEARCH *******************"); |
| 21 | + performBinarySearch(sortedValues, valueToBeSearched); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * <p>Binary Search |
| 26 | + * Few points to note here, |
| 27 | + * 1. Binary search works only on sorted collection |
| 28 | + * 2. Ex. Looking up a name in Telephone directory |
| 29 | + * 3. Each time we make a comparison, we eliminate half of the list </p> |
| 30 | + * |
| 31 | + * <p> Algorithm : |
| 32 | + * 1. Find the mid index in the list and compare it with the search term |
| 33 | + * 2. If search term is smaller then the element at mid index, eliminate upper half, else eliminate lower half |
| 34 | + * 3. If it is equal, break the loop as we have found our element. |
| 35 | + * 4. Keep running the loop until high >= low. |
| 36 | + * 5. return index of matching item, or -1 if not found |
| 37 | + * </p> |
| 38 | + * |
| 39 | + * <p>Complexity : |
| 40 | + * 1. Best Case : What is the fewer number of iterations to find the item? |
| 41 | + * => Best case is when search term is at the first try |
| 42 | + * => Number of comparisons in this case is 1 |
| 43 | + * |
| 44 | + * 2. Worst Case : What is the most number of comparisons needed to find the item? |
| 45 | + * => Worst case is when search term is not at all in the array |
| 46 | + * => If our array is of size N, we need N comparisons for worst case |
| 47 | + * |
| 48 | + * 3. Average Case : On an Average, how many comparisons are needed to find the element in the array? |
| 49 | + * => On an average, search term is anywhere in the list |
| 50 | + * </p> |
| 51 | + */ |
| 52 | + private static void performBinarySearch(int[] iListOfValues, int iValueToBeSearched) { |
| 53 | + int index = -1; |
| 54 | + int low = 0; |
| 55 | + int mid; |
| 56 | + int high = iListOfValues.length - 1; |
| 57 | + while (high >= low) { |
| 58 | + mid = (high + low)/2; |
| 59 | + if (iValueToBeSearched < iListOfValues[mid]) { |
| 60 | + high = mid - 1; |
| 61 | + } else if (iValueToBeSearched > iListOfValues[mid]) { |
| 62 | + low = mid + 1; |
| 63 | + } else { |
| 64 | + index = mid; |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + if (index != -1) { |
| 69 | + System.out.println("Element found in the list at position = " + index); |
| 70 | + } else { |
| 71 | + System.out.println("Element not found in the list"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments