forked from AaqilSh/DSA-Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
90 lines (79 loc) · 2.36 KB
/
BinarySearch.java
File metadata and controls
90 lines (79 loc) · 2.36 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class BinarySearch
{
public void bubbleSort(int[] arr)
{
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(arr[j-1] > arr[j])
{
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public boolean binarySearch(int[] arr, int n)
{
boolean found = false;
int L = 0, U = arr.length - 1, M = 0;
while(L<=U){
M = (L + U)/2;
if(n > arr[M]){
L = M + 1;
}
else if(n < arr[M]){
U = M - 1;
}
else{
found = true;
break;
}
}
if(found == true){
// System.out.println("Found element at"+M);
return true;
}
else{
// System.out.println("Not Found");
return false;
}
}
public static void main(String args[]) throws IOException
{
BinarySearch obj = new BinarySearch();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Determining size of the array
System.out.println("Enter the size of the array");
int size = Integer.parseInt(br.readLine());
// Array initialisation
int[] arr = new int[size];
System.out.println("Enter Array Elements");
for(int i = 0; i<arr.length; i++)
{
arr[i] = Integer.parseInt(br.readLine());
}
// Clears the Screen and asks for which number to search for
System.out.println("\fEnter the number to search for");
int num = Integer.parseInt(br.readLine());
// Sorting the array to let binarySearch() do it's work
obj.bubbleSort(arr);
// calling the binarySearch() method
if(obj.binarySearch(arr, num))
{
System.out.println("The element is found");
}
else
{
System.out.println("The element is not found");
}
}
}