-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBigO_nlogn.java
More file actions
27 lines (25 loc) · 775 Bytes
/
BigO_nlogn.java
File metadata and controls
27 lines (25 loc) · 775 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
/*
* Created by IntelliJ IDEA.
* User: divyanshb
* Date: 14/01/20
* Time: 8:35 AM
*/
package examples;
public class BigO_nlogn {
/**
* In this method, we can see that the first loop would be running
* n times, hence the complexity of that loop is O(n)
* But, the second loop is running only logn times, hence its complexity is
* O(logn)
* To calculate the total complexity of this algorithm, we will multiply the
* two complexities together
* Hence, we achieve O(n x logn) OR O(nlogn)
*/
public static void showComplexity(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j < 8; j = j * 2) {
System.out.println("i = " + i + " j = " + j);
}
}
}
}