-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
68 lines (64 loc) · 1.73 KB
/
BinarySearch.java
File metadata and controls
68 lines (64 loc) · 1.73 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
// Program: Binary Search Implementation
// Topic: Searching Algorithms (Arrays)
// Description: Performs binary search on a sorted integer array to find the position of a given element.
// Uses iterative logic to repeatedly divide the search interval in half until the element is found or not present.
package Searching;
import java.util.*;
/**
*
* @author Samim
*/
public class BinarySearch
{
public int searching(int ar[], int search)
{
int high=ar.length-1;
int low=0;
int flag=0;
while(low<=high)
{
int mid=(high+low)/2;
if(ar[mid]==search)
{
flag=mid+1;
break;
}
else if(ar[mid]<search)
{
low=mid+1;
}
else if(ar[mid]>search)
{
high=mid-1;
}
}
return flag;
}
public void display(int flag)
{
if(flag!=0)
{
System.out.println("Element Found at position:"+flag);
}
else
{
System.out.println("Element Not Found");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int ar[]=new int[10];
int search;
System.out.println("Enter The Elements Of Array :");
for(int i=0;i<10;i++)
{
ar[i]=sc.nextInt();
}
System.out.println("Enter Search Element :");
search=sc.nextInt();
BinarySearch obj=new BinarySearch();
int p=obj.searching(ar,search);
obj.display(p);
}
}