Skip to content

Commit 28b0326

Browse files
authored
GH-202: Big-O in Java (#203)
1 parent da46862 commit 28b0326

File tree

6 files changed

+557
-124
lines changed

6 files changed

+557
-124
lines changed

concepts/general/big-o.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Big O
22

3-
*See implementation in* [C++](../cpp/big-o.md), Java, [Python](../python/big-o.md), [Typescript](../typescript/big-o.md)
3+
*See implementation in* [C++](../cpp/big-o.md), [Java](../java/big-o/README.md), [Python](../python/big-o.md), [Typescript](../typescript/big-o.md)
44

55
> In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to perform.
66
@@ -11,26 +11,18 @@
1111
$O(1)$: Constant time, the time complexity of the algorithm is independent of the size of the input. Examples include accessing an element in an array by its index or performing a single operation on a single value.
1212

1313
```cpp
14-
#include <iostream>
15-
16-
using namespace std;
17-
18-
void doRandomStuff(){
19-
bool foo = true; // 1 operation
20-
int bar = 8 * 3; // 1 operation
21-
if(bar < 20){
22-
cout << "bar is small" << endl; // 1 opeartion
23-
}
24-
for(int i=0; i<bar; i++){
25-
// 24 operations
26-
cout << i << endl;
27-
}
28-
}
29-
int main() {
30-
doRandomStuff(); // O(1)
31-
doRandomStuff(); // O(1)
32-
doRandomStuff(); // O(1)
33-
}
14+
def do_random_stuff():
15+
foo = True # 1 operation
16+
bar = 8 * 3 # 1 operation
17+
if bar < 20: # 1 operation
18+
print("bar is small") # 1 operation
19+
for i in range(0, bar): # 24 operations
20+
print(i) # 1 operation
21+
22+
23+
do_random_stuff() # O(1)
24+
do_random_stuff() # O(1)
25+
do_random_stuff() # O(1)
3426
```
3527
3628
Run time for `doRandomStuff` is $O(1)$

concepts/java/big-o/README.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Big-O in Java
2+
3+
## Constant Run Time
4+
5+
$O(1)$: Constant time, the time complexity of the algorithm is independent of the size of the input. Examples include accessing an array element by index or pushing/popping from a stack.
6+
7+
8+
```Java
9+
void doRandomStuff(){
10+
boolean foo = true; // 1 operation
11+
int bar = 8 * 3;
12+
13+
if(bar < 20){
14+
System.out.println("bar is small");
15+
}
16+
17+
for(int i=0; i<bar; i++){
18+
System.out.println(i);
19+
}
20+
}
21+
22+
doRandomStuff() // O(1)
23+
```
24+
25+
0
26+
1
27+
2
28+
3
29+
4
30+
5
31+
6
32+
7
33+
8
34+
9
35+
10
36+
11
37+
12
38+
13
39+
14
40+
15
41+
16
42+
17
43+
18
44+
19
45+
20
46+
21
47+
22
48+
23
49+
50+
51+
## Linear Run Time
52+
53+
$O(n)$: Linear time, the time complexity of the algorithm increases linearly with the size of the input. Examples include linear search, traversing a list, or calculating the sum of an array.
54+
55+
56+
```Java
57+
boolean isContains(int target, int[] arr){
58+
for(int i=0; i<arr.length; i++){
59+
if(arr[i] == target){
60+
return true;
61+
}
62+
}
63+
return false;
64+
}
65+
```
66+
67+
68+
69+
70+
true
71+
72+
73+
74+
75+
```Java
76+
int[] arr;
77+
78+
arr = new int[]{1, 2, 3, 4, 5, 8, 10};
79+
System.out.println(isContains(8, arr)); // O(n)
80+
81+
arr = new int[]{1, 2, 3, 4, 5, 6, 7, 9, 10};
82+
System.out.println(isContains(8, arr)); // O(n)
83+
84+
arr = new int[]{1, 2, 100, 200, 300, 500};
85+
System.out.println(isContains(8, arr)); // O(n)
86+
87+
```
88+
89+
true
90+
false
91+
false
92+
93+
94+
Run time for `isContains` function is $O(n)$
95+
96+
## Quadaric Run Time
97+
98+
$O(n^2)$: Quadratic time, the time complexity of the algorithm increases as the square of the size of the input. Examples include bubble sort and selection sort.
99+
100+
101+
```Java
102+
void printPairs(int n){
103+
for(int i=1; i<=n; i++){
104+
for(int j=i+1; j<=n; j++){
105+
System.out.print("(" + i + ", " + j + ") ");
106+
}
107+
System.out.println();
108+
}
109+
}
110+
111+
printPairs(4)
112+
```
113+
114+
(1, 2) (1, 3) (1, 4)
115+
(2, 3) (2, 4)
116+
(3, 4)
117+
118+
119+
120+
$f(n) = n * (n-1) * 1$
121+
122+
$O(f(n)) = O(n^2)$
123+
124+
Run time for `printPairs` is $O(n^2)$
125+
126+
## Logarithm Run Time
127+
128+
$O(log n)$: Logirthm time describes an algorithm that performs a logarithmic number of operations, where the log is typically base 2. Examples include binary search and finding the height of a balanced binary tree.
129+
130+
131+
```Java
132+
boolean isContains(int target, int[] numbers){
133+
/* arr is sorted array */
134+
int lo = 0;
135+
int hi = numbers.length - 1;
136+
int mid;
137+
while(lo <= hi){
138+
mid = (lo + hi) / 2;
139+
if(numbers[mid] == target){
140+
return true;
141+
} else if(numbers[mid] < target){
142+
lo = mid + 1;
143+
} else {
144+
hi = mid - 1;
145+
}
146+
}
147+
return false;
148+
}
149+
150+
int[] numbers;
151+
152+
numbers = new int[]{1, 2, 3, 4, 5, 8, 10};
153+
System.out.println(isContains(8, numbers)); // O(log n)
154+
155+
numbers = new int[]{1, 2, 3, 4, 5, 9, 10};
156+
System.out.println(isContains(8, numbers)); // O(log n)
157+
158+
numbers = new int[]{8, 9, 10, 11, 12};
159+
System.out.println(isContains(8, numbers)); // O(log n)
160+
```
161+
162+
true
163+
false
164+
true
165+
166+
167+
$f(n) = 1 + 1 + 1 + 3log(n)= 3 + 3log(n)$
168+
169+
$O(f(n)) = O(log(n))$
170+
171+
Run time for `isContains` (with numbers is sorted array) is $O(log(n))$
172+
173+
## Combined Run Time
174+
175+
```java
176+
void combine = (int[] nums) => {
177+
foo(nums); // O(1)
178+
fuu(nums); // O(log(n))
179+
bar(nums); // O(n)
180+
baz(nums); // O(n^2)
181+
}
182+
183+
combine({1, 2, 3, 4, 5, 6}); // O(n^2)
184+
```
185+
186+
$f(n) = 1 + log(n) + n + n^2$
187+
188+
$O(f(n)) = O(n^2)$
189+
190+
Runtime for `combine()` is $O(n^2)$
191+
192+
## 🔗 Further Reading
193+
194+
* [Time complexity](https://en.wikipedia.org/wiki/Time_complexity), wikipedia
195+
* [Space complexity](https://en.wikipedia.org/wiki/Space_complexity), wikipedia
196+
* ▶️ [Asymptotic Notation](https://www.youtube.com/watch?v=iOq5kSKqeR4&ab_channel=CS50), CS50
197+
* ▶️ 2020, [Data Structures in Typescript #3 - Big-O Algorithm Analysis](https://www.youtube.com/watch?v=F2wwpDgoSoc&ab_channel=JeffZhang), Jeff Zhang

0 commit comments

Comments
 (0)