This repository was archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
60 lines (59 loc) · 1.29 KB
/
BinarySearch.java
File metadata and controls
60 lines (59 loc) · 1.29 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
56
57
58
59
60
import java.io.*;
class BinarySearch
{ void bin(){
try{
int i,j,temp,low,mid,high;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE VALUE OF NUM:");
int num=Integer.parseInt(br.readLine());
System.out.println("ENTER THE ELEMENTS OF ARRAY:");
int[] array=new int[num];
for(i=0;i<num;i++)
{
array[i]=Integer.parseInt(br.readLine());
}
for(i=0; i<num; i++)
{ for(j=0; j<(num-i-1); j++)
{ if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("SORTED ARRAY:");
for(i=0;i<num;i++)
{
System.out.println(array[i]);
}
System.out.println("ENTER THE ELEMENT TO BE SEARCHED:");
int search=Integer.parseInt(br.readLine());
low=0;
high=num-1;
do{
mid=(low+high)/2;
if(search<array[mid])
{
high=mid-1;
}
else if(search>array[mid])
{
low=mid+1;
}
}while(search!=array[mid] && low<=high);
if(search==array[mid])
{ System.out.println("SEARCH SUCCESFUL");
System.out.println("ELEMENT LOCATED AT:"+(mid+1));
}
else
System.out.println("NOT FOUND");
}
catch (Exception e){}
}//end of func
public static void main(String args[])
{
BinarySearch bs=new BinarySearch();
bs.bin();
}
}