-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinSA_re.java
32 lines (32 loc) · 1.32 KB
/
BinSA_re.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
import java.util.*;
public class BinSA_re{
public static int binSearch(int[] nums, int searchTarget, int left, int right){
if(right >= left && left <= nums.length - 1){
int median = ((left + right)/2) + 1;
System.out.println(median + " " + nums[median]);
int tempN = nums[median];
if(tempN == searchTarget){
return median;
}else if(tempN > searchTarget){
binSearch(nums, searchTarget, left, (median - 1));
}else if(tempN < searchTarget){
binSearch(nums, searchTarget, (median + 1), right);
}
}
return -1;
}
public static void main(String[] args){
int[] nums = new int[]{1,3,5,7,9,11,13,15,17,19};
int len = nums.length;
System.out.print("Enter the number to be searched: ");
Scanner sc = new Scanner(System.in);
int searchTarget = sc.nextInt();
sc.close();
int targetIndex = binSearch(nums, searchTarget, 0, (len - 1));
if(targetIndex == -1){
System.out.println("The element "+ searchTarget +" is not present in the array");
}else{
System.out.println("The index of "+ searchTarget +" in the array is: "+ (targetIndex + 1) +".");
}
}
}