-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpart1-timecomplexity.js
More file actions
61 lines (51 loc) · 2.21 KB
/
part1-timecomplexity.js
File metadata and controls
61 lines (51 loc) · 2.21 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
let n = 10;
function TimeComplexity_SingleForLoopPlusPlus() {
for(var i=1;i<=n;i++) {
console.log(i);
}
console.log("Example 1 - Maximum Time Complexity because its repeating for N times: O(n)");
}
function TimeComplexity_SingleForLoopMinusMinus() {
for(var i=n;i>=1;i--) {
console.log(i);
}
console.log("Example 2 - Maximum Time Complexity because its repeating for N times: O(n)");
}
function TimeComplexity_SingleForLoop_Multiply() {
for(var i=1;i<=n;i=i*2) {
console.log(i);
}
console.log("Example 3 - Maximum Time Complexity log n mean it will give decimal, float value: O(log n base 2)");
}
function TimeComplexity_SingleForLoop_Divided() {
for(var i=n;i>=1;i=i/2) {
console.log(i);
}
console.log("Example 4 - Maximum Time Complexity log n mean it will give decimal, float value: O(log n base 2)");
}
function TimeComplexity_SingleForLoop_RootNTimes() {
for(var i=1;i*i<n;i++) {
console.log(i);
}
console.log("Example 5 - Maximum Time Complexity root N times: O(√n)");
//Explanation -
// i*i < n (Contidition is given in the program)
// i*i >= n (The condition will terminated if its greater than or equal to n)
// i square 2 >= n (Because i*i = i square 2)
// i >= √n (Because we know if we send square 2 opposite side its √n)
// So the complexity is O(√n)
}
function TimeComplexity_DoubleForLoop() {
for(var i=1;i<=n;i++) {
for(var j=1;j<=n;j++) {
console.log(i," ------> ", j);
}
}
console.log("Example 6 - Time Complexity its repeating for N2 times: O(N2) -- N Square Quadratic");
}
TimeComplexity_SingleForLoopPlusPlus(); //Applicable to increment or decrement increment or decrement the i value
TimeComplexity_SingleForLoopMinusMinus(); //Applicable to increment or decrement increment or decrement the i value
// TimeComplexity_DoubleForLoop(); //Applicable to increment or decrement increment or decrement increment or decrement the i value
// TimeComplexity_SingleForLoop_Multiply(); //Applicable to Multiple the i value
// TimeComplexity_SingleForLoop_Divided(); //Applicable to Divided the i value
// TimeComplexity_SingleForLoop_RootNTimes(); //Root N times