forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearch.java
92 lines (69 loc) · 1.77 KB
/
binarySearch.java
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
90
91
92
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package method3;
//import java.util.Arrays;
/**
*
* @author Chamodi
*/
public class Method3 {
int binarySearch(int element){
int setA[] = {12,34,11,9,3};
int temp;
for (int i = 0; i < setA.length; i++) {
for (int j = i; j > 0; j--) {
if (setA[j] < setA[j - 1]) {
temp = setA[j];
setA[j] = setA[j - 1];
setA[j - 1] = temp;
}
}
}
// for (int i = 0; i < setA.length; i++) {
// System.out.println(setA[i]);
// }
int first=0;
int last=setA.length-1;
// System.out.println(last);
while(last>=first){
int mid=(first+last)/2;
if(setA[mid]==element){
return 1;
}
else if(setA[mid]>element){
last=mid-1;
}else{
first=mid+1;
}
}
return -1;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Method3 bs=new Method3();
// int setA[] = {12,34,11,9,3};
int setB[] = {2,1,3,5};
// int n=setA.length;
//
int count=0;
for (int i = 0; i < setB.length; i++){
int c;
c = bs.binarySearch(setB[i]);
if(c==-1){
}else{
count++;
}
}
if(count==1){
System.out.println("Common element");
}else{
System.out.println("No common element");
}
// Arrays.sort(setA);
}
}