Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 27, 2024. It is now read-only.

Commit 702c2ca

Browse files
Merge pull request #25 from soumidas212003/main
Binary Search
2 parents dca4b9a + c1627c7 commit 702c2ca

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

‎JAVA/Binary_Search.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Scanner;
2+
public class Binary_Search {
3+
public static int BinarySearch(int arr[], int key){
4+
int start=0,end=arr.length-1;
5+
while(start<=end){
6+
int mid=(start+end)/2;
7+
if(arr[mid]==key){
8+
return mid;
9+
}
10+
if(arr[mid]<key){
11+
start=mid+1;
12+
}
13+
else{
14+
end=mid-1;
15+
}
16+
}
17+
return -1;
18+
}
19+
20+
public static void main(String[] args) {
21+
Scanner sc=new Scanner(System.in);
22+
System.out.print("Enter The Size Of The Array:- ");
23+
int size=sc.nextInt();
24+
int arr[]=new int[size];
25+
for(int i=0;i<size;i++){
26+
System.out.print("Enter Arr["+i+"]:- ");
27+
int x=sc.nextInt();
28+
arr[i]=x;
29+
}
30+
System.out.println("The Array Is:- ");
31+
for(int i=0;i<size;i++){
32+
System.out.print(arr[i]+",");
33+
}
34+
System.out.print("\nEnter The String You Want To Search:- ");
35+
int key=sc.nextInt();
36+
int x=BinarySearch(arr, key);
37+
if(x < 0){
38+
System.out.print("\nYour Given Key Is Not Present In The Array");
39+
}
40+
else{
41+
System.out.println("Your Key Is Present In The Array At The Position:- "+x);
42+
}
43+
sc.close();
44+
}
45+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /