-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructureTest.java
79 lines (72 loc) · 1.75 KB
/
StructureTest.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
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
72
73
74
75
76
77
78
79
public class StructureTest
{
public static int sum(int arr[]) {
int i, total=0;
int n = arr.length;
for(i=0; i<n; i++){
total += arr[i];
}
return total;
}
public static void binsrch(int a[], int x) {
int lower = 1;
int upper = a.length + 1;
int mid;
while(lower <= upper) {
mid = (lower + upper) / 2;
if(x > a[mid])
lower = mid + 1;
else if(x < a[mid])
upper = mid - 1;
else {
System.out.println("Found, " + x + "is" + mid + "record.");
break;
}
}
}
public static int fibonacci(int n) {
if (n == 0)
return 0;
else if (n==1)
return 1;
else
return(fibonacci(n-1) + fibonacci(n-2));
}
public static int fibonacci2(int n) {
int prev1, prev2, item = 0, i;
if (n==0)
return 0;
else if (n==1)
return 1;
else {
prev2 = 0;
prev1 = 1;
for(i=2; i<=n; i++) {
item = prev1 + prev2;
prev2 = prev1;
prev1 = item;
}
return item;
}
}
public static void main(String[] args) {
long startTime = System.nanoTime();
long endTime = System.nanoTime();
long duration = (endTime - startTime);
System.out.println("Hello! World");
int[] arr = {1,2, 6, 4, 5, 3, 7, 8};
int arr_sum = sum(arr);
System.out.println(arr_sum);
binsrch(arr, 3);
startTime = System.nanoTime();
System.out.println(fibonacci(30));
endTime = System.nanoTime();
duration = (endTime - startTime);
System.out.println("execute time:" + duration);
startTime = System.nanoTime();
System.out.println(fibonacci2(30));
endTime = System.nanoTime();
duration = (endTime - startTime);
System.out.println("execute time:" + duration);
}
}