|
| 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