DS & Algorithms Flashcards
What is an algorithm?
An algorithm is just the steps you take to solve a problem
A step-by-step procedure that defines a set of instructions that must be carried out in a specific order to produce the desired result
An algorithm is just a function
When worrying about real data, what 2 things do you have to worry about?
Space and time
What is Big-O notation?
It mathematically describes the complexity of an algorithm in terms of time and SPCE
Name the 4 big O time complexities
Quadratic O(n^2), linear O(n), logarithmic O(log n), constant time O(1)
O(1)
For all inputs to our algorithm there is and will always be only one operation required
- Order 1
- Constant Time
O(1) Example
const nums = [1,2,3,4,5]
const firstNumber = nums[0]
No matter how many inputs are located in num, there will only ever be one operation needed!
O(N)
For all inputs to our algorithm there will be one operation per input
- Order N
- Linear
- Linear Scaling
O(N) Example
const nums = [1,2,3,4,5]
let sum = 0;
for (let num of nums) {
sum += num
}
What is space complexity?
How much memory is used
What is time complexity?
How many primitive operations are executed