Skip to content

Commit 8a9210d

Browse files
author
Prakharnagore000
committed
big o notation
0 parents commit 8a9210d

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// To analyze the performance of an algorithm, we use Big O Notation
2+
// Big O Notation can give us a high level understanding of the time or space complexity of an algorithm
3+
// big o notation is a way to formalize fuzzy counting
4+
// it allows us to talk formally about how the runtime of an algorithm grows as the inputs grow
5+
// Example - O(1), O(n), O(n*n)
6+
7+
// Time Complexity
8+
// it allows us to talk formally about how the runtime of an algorithm grows as the inputs grow
9+
// Big O Shorthands
10+
// Arithmetic operations are constant
11+
// Variable assignment is constant
12+
// Accessing elements in an array (by index) or object (by key) is constant
13+
// In a loop, the the complexity is the length of the loop times the complexity of whatever happens inside of the loop
14+
15+
// Big O Chart
16+
// O(n!)
17+
// O(2^n)
18+
// O(n^2)
19+
// O(n log n)
20+
// O(n)
21+
// O(log n) or O(1)
22+
23+
// Space Complexity
24+
// how much additional memory do we need to allocate in order to run the code in our algorithm?
25+
// Rules of Thumb
26+
// Most primitives (booleans, numbers, undefined, null) are constant space
27+
// Strings require O(n) space (where n is the string length)
28+
// Reference types are generally O( n), where n is the length (for arrays) or the number of keys (for objects)
29+
// The time or space complexity (as measured by Big O) depends only on the algorithm, not the hardware used to run the algorithm
30+
31+
// Performance Tracker Tool
32+
// https://rithmschool.github.io/function-timer-demo/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function addUpTo(n) {
2+
let total = 0;
3+
for (let i = 1; i <= n; i++) {
4+
total += i;
5+
}
6+
return total;
7+
}
8+
9+
var t1 = performance.now();
10+
addUpTo(1000000000);
11+
var t2 = performance.now();
12+
console.log(`Time Elapsed: ${(t2 - t1) / 1000} seconds.`);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function addUpTo(n) {
2+
return (n * (n + 1)) / 2;
3+
}
4+
5+
var time1 = performance.now();
6+
addUpTo(1000000000);
7+
var time2 = performance.now();
8+
console.log(`Time Elapsed: ${(time2 - time1) / 1000} seconds.`);

README.md

152 Bytes

Data-Structure-And-Algorithm-Notes

Syllabus

Big O Notation

0 commit comments

Comments
 (0)