Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Latest commit

 

History

History
History
48 lines (36 loc) · 1.01 KB

File metadata and controls

48 lines (36 loc) · 1.01 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
class BinarySearch {
//Function using binary search algo in recursive method
int binarysearch(int[] arr,int l,int r,int x)
{
if(r>=l)
{
int mid=l+(r-l)/2;
if(arr[mid]==x)
return mid;
if(arr[mid]>x)
return binarysearch(arr,l,mid-1,x);
return binarysearch(arr,mid+1,r,x);
}
return -1;
}
//Main function is used to take inputs
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
BinarySearch obj=new BinarySearch();
System.out.print("Enter number of elements in array:");
int n=input.nextInt();
int[] arr=new int[n];
System.out.println("Enter elements of the array:");
for(int i=0;i<n;i++)
arr[i]=input.nextInt();
System.out.print("Enter element to be searched:");
int el=input.nextInt();
int result = obj.binarysearch(arr,0,n-1,el);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index "+result);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.