|
| 1 | +package com.baeldung.algorithms.analysis; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +public class AnalysisRunnerLiveTest { |
| 6 | + |
| 7 | + int n = 10; |
| 8 | + int total = 0; |
| 9 | + |
| 10 | + @Test |
| 11 | + public void whenConstantComplexity_thenConstantRuntime() { |
| 12 | + |
| 13 | + System.out.println("**** n = " + n + " ****"); |
| 14 | + System.out.println(); |
| 15 | + |
| 16 | + // Constant Time |
| 17 | + System.out.println("**** Constant time ****"); |
| 18 | + |
| 19 | + System.out.println("Hey - your input is: " + n); |
| 20 | + System.out.println("Running time not dependent on input size!"); |
| 21 | + System.out.println(); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void whenLogarithmicComplexity_thenLogarithmicRuntime() { |
| 26 | + // Logarithmic Time |
| 27 | + System.out.println("**** Logarithmic Time ****"); |
| 28 | + for (int i = 1; i < n; i = i * 2) { |
| 29 | + // System.out.println("Hey - I'm busy looking at: " + i); |
| 30 | + total++; |
| 31 | + } |
| 32 | + System.out.println("Total amount of times run: " + total); |
| 33 | + System.out.println(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void whenLinearComplexity_thenLinearRuntime() { |
| 38 | + // Linear Time |
| 39 | + System.out.println("**** Linear Time ****"); |
| 40 | + for (int i = 0; i < n; i++) { |
| 41 | + // System.out.println("Hey - I'm busy looking at: " + i); |
| 42 | + total++; |
| 43 | + } |
| 44 | + System.out.println("Total amount of times run: " + total); |
| 45 | + System.out.println(); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void whenNLogNComplexity_thenNLogNRuntime() { |
| 51 | + // N Log N Time |
| 52 | + System.out.println("**** nlogn Time ****"); |
| 53 | + total = 0; |
| 54 | + for ( |
| 55 | + |
| 56 | + int i = 1; i <= n; i++) { |
| 57 | + for (int j = 1; j < n; j = j * 2) { |
| 58 | + // System.out.println("Hey - I'm busy looking at: " + i + " and " + j); |
| 59 | + total++; |
| 60 | + } |
| 61 | + } |
| 62 | + System.out.println("Total amount of times run: " + total); |
| 63 | + System.out.println(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void whenQuadraticComplexity_thenQuadraticRuntime() { |
| 68 | + // Quadratic Time |
| 69 | + System.out.println("**** Quadratic Time ****"); |
| 70 | + total = 0; |
| 71 | + for ( |
| 72 | + |
| 73 | + int i = 1; i <= n; i++) { |
| 74 | + for (int j = 1; j <= n; j++) { |
| 75 | + // System.out.println("Hey - I'm busy looking at: " + i + " and " + j); |
| 76 | + total++; |
| 77 | + } |
| 78 | + } |
| 79 | + System.out.println("Total amount of times run: " + total); |
| 80 | + System.out.println(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void whenCubicComplexity_thenCubicRuntime() { |
| 85 | + // Cubic Time |
| 86 | + System.out.println("**** Cubic Time ****"); |
| 87 | + total = 0; |
| 88 | + for ( |
| 89 | + |
| 90 | + int i = 1; i <= n; i++) { |
| 91 | + for (int j = 1; j <= n; j++) { |
| 92 | + for (int k = 1; k <= n; k++) { |
| 93 | + // System.out.println("Hey - I'm busy looking at: " + i + " and " + j + " and " + k); |
| 94 | + total++; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + System.out.println("Total amount of times run: " + total); |
| 99 | + System.out.println(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void whenExponentialComplexity_thenExponentialRuntime() { |
| 104 | + // Exponential Time |
| 105 | + System.out.println("**** Exponential Time ****"); |
| 106 | + total = 0; |
| 107 | + for ( |
| 108 | + |
| 109 | + int i = 1; i <= Math.pow(2, n); i++) { |
| 110 | + // System.out.println("Hey - I'm busy looking at: " + i); |
| 111 | + total++; |
| 112 | + } |
| 113 | + System.out.println("Total amount of times run: " + total); |
| 114 | + System.out.println(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void whenFactorialComplexity_thenFactorialRuntime() { |
| 119 | + // Factorial Time |
| 120 | + System.out.println("**** Factorial Time ****"); |
| 121 | + total = 0; |
| 122 | + for ( |
| 123 | + |
| 124 | + int i = 1; i <= |
| 125 | + |
| 126 | + factorial(n); i++) { |
| 127 | + // System.out.println("Hey - I'm busy looking at: " + i); |
| 128 | + total++; |
| 129 | + } |
| 130 | + System.out.println("Total amount of times run: " + total); |
| 131 | + } |
| 132 | + |
| 133 | + static int factorial(int n) { |
| 134 | + if (n == 0 || n == 1) |
| 135 | + return 1; |
| 136 | + else |
| 137 | + return n * factorial(n - 1); |
| 138 | + } |
| 139 | +} |
0 commit comments