-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
55 lines (50 loc) · 1.31 KB
/
BinarySearch.java
File metadata and controls
55 lines (50 loc) · 1.31 KB
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
49
50
51
52
53
54
55
//Program to find element from an sorted array using binary search.
import java.util.Scanner;
public class BinarySearch
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Size of the Array :- ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter the Array :- ");
for(int i=0; i<size; i++)
{
System.out.println("Enter the "+(i+1)+" element :- ");
arr[i] = sc.nextInt();
}
System.out.println("Entered Array is :- ");
for(int i=0;i<size;i++)
{
System.out.print("\t"+arr[i]);
}
//Binary search
System.out.print("\nEnter the number which you want to search: ");
int key = sc.nextInt();
int first = 0;
int last = arr.length - 1;
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 : " + mid);
break;
}
else
{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last )
{
System.out.println("Element is not found!");
}
}
}