Skip to content

Commit a6b2996

Browse files
Completed constant, linear, and quadratic time complexities
1 parent a72a47d commit a6b2996

File tree

8 files changed

+157
-2
lines changed

8 files changed

+157
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
deno_dir

Big-O/Ex1.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Exercise 1 -----------------------------------------
2+
3+
function anotherFunction() {
4+
// unknown runtime
5+
}
6+
7+
function funChallenge(input: Array<any>): number {
8+
let a = 10; // O(1)
9+
a = 50 + 3; // O(1)
10+
11+
for (let i = 0; i < input.length; i++) { // O(n)
12+
anotherFunction(); // O(n)
13+
let stranger = true; // O(n)
14+
a++; // O(n)
15+
}
16+
17+
return a; // O(1)
18+
}
19+
20+
21+
22+
const numbersArr = [1,2,3,4,5];
23+
24+
funChallenge(numbersArr); // Time Complexity: O(3 + 4n) ~ O(n)

Big-O/Ex2.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Exercise 2 -----------------------------------------
2+
3+
function anotherFunChallenge(input: number): void {
4+
let a = 5; // O(1)
5+
let b = 10; // O(1)
6+
let c = 50; // O(1)
7+
8+
for (let i = 0; i < input; i++) { // O(n)
9+
let x = i + 1; // O(n)
10+
let y = i + 2; // O(n)
11+
let z = i + 3; // O(n)
12+
}
13+
14+
for (let j = 0; j < input; j++) { // O(n)
15+
let p = j * 2; // O(n)
16+
let q = j * 2; // O(n)
17+
}
18+
19+
let whoAmI = "I don't know"; // O(1)
20+
}
21+
22+
anotherFunChallenge(7); // Time Complexity: O(4 + 7n) ~ O(n)

Big-O/Hello_Big_O.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
const nemo = ['nemo'];
3+
const fishColony = ['dory', 'bruce', 'marlin', 'nemo', 'gill', 'bloat', 'nigel', 'squirt', 'darla', 'hank'];
4+
const largeArr = new Array(10000000).fill('squish');
5+
6+
function findNemo(fishes: Array<string>): string {
7+
let t0 = performance.now(); // O(1)
8+
9+
let isNemoFound = false; // O(1)
10+
11+
for (let i = 0; i < fishes.length; i++) { // O(n)
12+
if (fishes[i] === 'nemo') { // O(n)
13+
isNemoFound = true; // O(n)
14+
break; // O(1)
15+
}
16+
}
17+
18+
let t1 = performance.now(); // O(1)
19+
console.log("Call to find Nemo took " + (t1 - t0) + "ms."); // O(1)
20+
21+
return isNemoFound ? "Found NEMO!" : "Where did Nemo go?!"; // O(1)
22+
}
23+
24+
function getFirstFish(fishes: Array<string>) {
25+
return fishes[0].charAt(0).toUpperCase() + fishes[0].substring(1).toLowerCase(); // O(1)
26+
}
27+
28+
29+
30+
console.log(findNemo(fishColony)); // Time Complexity: O(n)
31+
console.log(findNemo(largeArr)); // Time Complexity: O(n)
32+
console.log(getFirstFish(fishColony), "just keeps swimming!"); // Time Complexity: O(1)

Big-O/README.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
1-
# Big O Notation
1+
# Algorithm Time-Complexity Analysis
2+
**Goal:** Understand how the runtime of an algorithm is affected by an increasing number of elements.
3+
4+
## 5 Rules
5+
1. Analyze the worst case performance of the algorithm, i.e. Big O
6+
2. Add steps in order (+); multiply nested steps (*)
7+
3. Different inputs should have different variables, e.g. O(a+b)
8+
4. Remove constants
9+
5. Drop non-dominants
10+
11+
## 3 Types
12+
### 1. Big O – Worst Case
13+
14+
#### Ideal
15+
O(1) – Constant
16+
O(log n) – Logarithmic
17+
O(n) – Linear
18+
19+
#### Acceptable
20+
O(n * log n) – Log Linear
21+
22+
#### Avoid
23+
O(n^2) – Quadratic
24+
O(2^n) – Exponential
25+
O(n!) – Factorial
26+
27+
### 2. Big Θ – Average/Tight Case
28+
### 3. Big Ω – Best Case
29+
30+
## Resources
31+
32+
- [Big-O Algorithm Complexity Cheat Sheet (Know Thy Complexities!) @ericdrowell](https://www.bigocheatsheet.com/ "Big O Cheat Sheet")
33+
- [Practical Java Examples of the Big O Notation](https://www.baeldung.com/java-algorithm-complexity "Big O Examples")

Big-O/Rule3.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Rule 3: Different inputs should have different variables.--------------------
2+
3+
function compressBoxes(fullBoxes: Array<string>, emptyBoxes: Array<string>) {
4+
5+
fullBoxes.forEach((box) => { // O(a)
6+
console.log(box);
7+
});
8+
9+
emptyBoxes.forEach((box) => { // O(b)
10+
console.log(box);
11+
});
12+
}
13+
14+
// Time Complexity: O(a+b)

Big-O/n_squared.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Examples of Quadratic Big O Runtime — O(n^2) ------------------
2+
3+
const boxes = ["a", "b", "c", "d", "e"];
4+
5+
function logAllPairs(array: Array<any>): void {
6+
for (let i = 0; i < array.length; i++) { // O(n)
7+
for (let j = 0; j < array.length; j++) { // O(n)
8+
console.log(array[i], array[j]); // O(n)
9+
}
10+
}
11+
}
12+
13+
function printNumbersThenPairSums(numbers: Array<number>): void {
14+
console.log("these are the numbers:");
15+
numbers.forEach(function (number) { // O(n)
16+
console.log(number);
17+
});
18+
19+
console.log("and these are their sums:");
20+
numbers.forEach(function (firstNumber) { // O(n)
21+
numbers.forEach(function (secondNumber) { // O(n)
22+
console.log(firstNumber + secondNumber); // O(n)
23+
});
24+
});
25+
}
26+
27+
28+
logAllPairs(boxes); // O(n * 2n) ~ O(n^2)
29+
30+
printNumbersThenPairSums([1, 2, 3, 4, 5]); // O(n + n*2n) ~ O(n^2)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Data Structures & Algorithms in Typescript
1+
# Data Structures & Algorithms in TypeScript

0 commit comments

Comments
 (0)