-
Notifications
You must be signed in to change notification settings - Fork 2
/
RecursionSearch.java
45 lines (41 loc) · 1.08 KB
/
RecursionSearch.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
/*
* Alt + Shift + F to indent lines.
*/
package DataStructure;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Vinayak
*/
public class RecursionSearch {
public static void main(String[] args) {
Scanner s1=new Scanner(System.in);
System.out.println("Enter size");
int n=s1.nextInt();
System.out.println("Enter array");
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=s1.nextInt();
Arrays.sort(arr);
System.out.println("Enter no to search");
int d=s1.nextInt();
if(search(arr,d,0,arr.length-1)<0)
System.out.println("Not found");
else
System.out.println("Found at "+search(arr,d,0,arr.length-1));
}
static int search(int arr[],int d,int l,int h){
if(l<=h)
{
int mid=(l+h)/2;
if(arr[mid]==d)
return mid;
if(arr[mid]<d)
return search(arr,d,mid+1,h);
else
return search(arr,d,1,mid-1);
}
return -1;
}
}