-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchRandomSortedIPK.java
More file actions
71 lines (69 loc) · 2.09 KB
/
Copy pathSearchRandomSortedIPK.java
File metadata and controls
71 lines (69 loc) · 2.09 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
69
70
71
import java.util.*;
public class SearchRandomSortedIPK{
public static void main(String[] args){
System.out.print("Hasilkan IPK sebanyak: ");
Scanner Input1= new Scanner(System.in);
int myInput= Input1.nextInt();
double[] GPA= new double[myInput];
for(int a=0;a<GPA.length;a++){
GPA[a]= Math.round((Math.random()*3+1)*100)/100.00;
}
for(int a=0;a<GPA.length;a++){
System.out.print(GPA[a]+" ");
}
System.out.println();
Scanner Input2= new Scanner(System.in);
double searchInput=0;
boolean checkInput=false;
while(!checkInput){
try {
System.out.print("Cari IPK : ");
searchInput= Input2.nextDouble();
checkInput=true;
} catch (InputMismatchException e) {
System.out.println("Input salah!");
Input2.next();
}
}
Arrays.sort(GPA);
BinarySearch(searchInput,GPA);
Input1.close();
Input2.close();
}
public static void BinarySearch(double searchInput,double GPA[]){
boolean found=false;
int i=0,j=GPA.length,k=0,result=1;
double query=searchInput;
while(!found&&i<=j){
k=(i+j)/2;
if(GPA[k]<query){
i=k+1;
}
else if(GPA[k]==query){
int up=k-1,down=k+1;
while(!found){
if(GPA[up]==GPA[k]){
result++;
}
else if(GPA[down]==GPA[k]){
result++;
}
else{
found=true;
}
up--;
down++;
}
}
else{
j=k-1;
}
}
if(found==false){
System.out.println("IPK tidak ditemukan");
}
else{
System.out.println("IPK " + query +" ditemukan sebanyak "+ result +" kali");
}
}
}