Answers for "write algorithm for binary search java"

1

binary search program in java

public class Main{ public static void binarySearch(int arr[], int a, int b, int key){ int mid = (a + b)/2; while( a <= b ){ if ( arr[mid] < key ){ a = mid + 1; }else if ( arr[mid] == key ){ System.out.println("number is here " + mid); break; }else{ b = mid - 1; } mid = (a + b)/2; } if ( a > b ){ System.out.println("number not here"); } } public static void main(String args[]){ int arr[] = {10,20,30,40,50}; int key = 30; int b=arr.length-1; binarySearch(arr,0,b,key); } }
Posted by: Guest on June-29-2021

Code answers related to "write algorithm for binary search java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language