Matthew Bodily
Lecture Slides for DevMountain's Web Development Course
Computer Science
A particular way to organize data so that it can be used efficiently
Interfaces
Using the back and forward button on browser is a common example of stack
amazon.com
espn.com
stackoverflow.com
github.com
backcountry.com
myspace.com
Push
Pop
The LAST item IN would be the FIRST item OUT
ebay.com
devmountain.com
hackernoon.com
grabient.com
coolors.co
namecheap.com
Managing multiple users who are looking to buy the last item in stock
Enqueue
Dequeue
The FIRST item IN would become the FIRST item OUT
A particular way to organize data so that it can be used efficiently
Implementations
Made up of nodes that point to the next node. Each node has two parts; 1: A value, 2: A pointer to the next node
next
Object
next
Object
next
Object
Head
Tail
O
"Tree" of nodes where the node to the left of the initial node is less and the node to the right is more
8
3
10
1
6
4
12
7
11
13
Big O is an algorithm to determine the complexity/efficiency of your code.
Can be calculated as O(n) with n representing the variable of time.
O(1)
This formula means that the complexity of the code does not change no matter the size of the input
function findFirst(input){
return input[0]
}
O(n)
This formula means that the complexity of the code changes linearly in regards to input size (because we have to loop)
function findThree(input, value){
for(let i = 0; i < input.length; i++){
if(input[i] === value){
return `found ${value}!`
}
}
return `value not found`
}
findThree([3,1,2], 3) // 1 iteration (as good as it gets)
findThree([0,1,2,4], 3) // 4 iterations (not so good but four loops isn't too bad)
findThree([0,1,2,REALY LONG ARRAY, 3], 3) // who knows. 3 is at the end so.. (as bad as it gets)
O(N2)
This formula means that the complexity of the code changes based on the squared input (because we have nested loops)
function makeTuples(input) {
let answer = [];
for (let i = 0; i < input.length; i++) {
for (let j = 0; j < input.length; j++) {
answer.push([input[i], input[j]]);
}
}
return answer;
}
O(log n)
This formula is more complex and gets pretty deep into some math.
Imagine a scenario where you needed to find a name in a phone book. You half the phone book and look for it there. If you find it, return the name, if not, half the phone book again with your just searched half. Continue this process until you find the name.
Essentially you can count the number of loops and that will be your value for n. The more loops, the more complex it will be to perform with regards to the size of your input.
There are many sorting algorithms available to you. Some are better than others in different situations. Because algorithm A works for one data set does not mean that it is best for another data set
Loops over the entire input and compares the first value to the next one. If it is out of order, it swaps them. Repeats this process until entire input is sorted.
Very similar to Bubble. Loops over the entire input and compares the first value to the next one. If it is out of order, moves index to beginning of input. Repeats this process until entire input is sorted.
Divide and conquer! Split the input into halves and sort them recursively, or calls itself until nothing is left. Once the halves are sorted, it will sort them.
Takes last element from input and assigns it as the "pivot", or element in which you will compare future element. If first element is smaller than pivot it gets put into a "left" list. If element is bigger it goes "right". If the first element is not larger than the pivot, the pivot moves to the left one.
Each list is sorted independently via recursion. The left list is then concatenated with the pivot and then that newly formed list is concatenated with the right list
function fibonacci(num) {
if (num <= 1) return 1
return fibonacci(num - 1) + fibonacci(num - 2)
}
fibonacci(2) // 2
fibonacci(4) // 5
fibonacci(7) // 21
fibonacci(9) // 55
function factorial(num) {
if (n === 1) {
return 1
}
return n * factorial(n - 1)
}
factorial(5) // 120
factorial(3) // 6
factorial(7) // 5040
factorial(12) // 479001600
By Matthew Bodily