forked from onlybooks/java-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigOExample.java
77 lines (67 loc) · 1.53 KB
/
BigOExample.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
package ch05;
public class BigOExample {
public int O_1(int n) {
return 42;
}
public int O_logn(int n) {
while (n > 1)
n /= 2;
return n;
}
public int O_n(int n) {
int r = 0;
for (int i = 0; i < n; i++)
r++;
return r;
}
public int O_nlogn(int n) {
int r = n;
for (int i = 0; i < n; i++) {
while (n > 1)
n /= 2;
n = r;
}
return r;
}
public int O_n2(int n) {
int r = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
r += j;
}
return r;
}
public int O_2n(int n) {
if (n <= 1)
return n;
else
return O_2n(n - 1) + O_2n(n - 2);
}
public int O_nfact(int n) {
for (int i = 0; i < n; i++) {
O_nfact(n - 1);
}
return n;
}
static int count = 0;
static public int factorial(int n) {
if (n >= 1) {
count++;
return n * factorial(n - 1);
} else {
count++;
return 1;
}
}
public static void main(String[] args) {
count = 0;
factorial(5);
System.out.printf("factorial(5), count = %d\n", count);
count = 0;
factorial(6);
System.out.printf("factorial(6), count = %d\n", count);
count = 0;
factorial(7);
System.out.printf("factorial(7), count = %d\n", count);
}
}