forked from saisarathganti/DataStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathternarysearch.java
More file actions
44 lines (39 loc) · 819 Bytes
/
ternarysearch.java
File metadata and controls
44 lines (39 loc) · 819 Bytes
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
import java.util.Scanner;
public class ternarysearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int size=s.nextInt();
int[] arr=new int[size];
for(int i=0;i<size;i++) {
arr[i]=s.nextInt();
}
int mid1,mid2;
System.out.println("enter the key");
int key=s.nextInt();
int first=0,last=size-1;
while(first<=last) {
mid1=(first+last)/3;
mid2=((first+last)/3)*2;
if(arr[mid1]==key) {
System.out.println("mid:"+mid1);
return;
}
if(arr[mid2]==key) {
System.out.println("mid:"+mid2);
return;
}
if(arr[mid1]>key) {
last=mid1-1;
}
else if(arr[mid2]<key) {
first=mid2+1;
}
else {
first=mid1+1;
last=mid2-1;
}
}
System.out.println("element not found");
}
}