MCPcopy Create free account
hub / github.com/SR-Sunny-Raj/Hacktoberfest2021-DSA / binarySearch

Method binarySearch

05. Searching/binarySearch.java:18–56  ·  view source on GitHub ↗
(int element)

Source from the content-addressed store, hash-verified

16 */
17public class Method3 {
18 int binarySearch(int element){
19 int setA[] = {12,34,11,9,3};
20
21
22 int temp;
23 for (int i = 0; i < setA.length; i++) {
24 for (int j = i; j > 0; j--) {
25 if (setA[j] < setA[j - 1]) {
26 temp = setA[j];
27 setA[j] = setA[j - 1];
28 setA[j - 1] = temp;
29 }
30 }
31 }
32
33// for (int i = 0; i < setA.length; i++) {
34// System.out.println(setA[i]);
35// }
36
37
38 int first=0;
39 int last=setA.length-1;
40// System.out.println(last);
41
42 while(last>=first){
43 int mid=(first+last)/2;
44 if(setA[mid]==element){
45 return 1;
46 }
47 else if(setA[mid]>element){
48 last=mid-1;
49 }else{
50 first=mid+1;
51 }
52
53
54 }
55 return -1;
56 }
57
58
59

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected