From 2e36672e412e1e9e2082009e262fda2f671cb872 Mon Sep 17 00:00:00 2001 From: AaryaB11 <73346599+AaryaB11@users.noreply.github.com> Date: Fri, 23 Oct 2020 19:14:03 +0530 Subject: [PATCH] Binary Search --- Binary Search | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Binary Search diff --git a/Binary Search b/Binary Search new file mode 100644 index 0000000..0bc669e --- /dev/null +++ b/Binary Search @@ -0,0 +1,25 @@ +class BinarySearchExample{ + public static void binarySearch(int arr[], int first, int last, int key){ + int mid = (first + last)/2; + while( first <= last ){ + if ( arr[mid] < key ){ + first = mid + 1; + }else if ( arr[mid] == key ){ + System.out.println("Element is found at index: " + mid); + break; + }else{ + last = mid - 1; + } + mid = (first + last)/2; + } + if ( first > last ){ + System.out.println("Element not found!"); + } + } + public static void main(String args[]){ + int arr[] = {10,20,30,40,50}; + int key = 30; + int last=arr.length-1; + binarySearch(arr,0,last,key); + } +}