Computer Science
Data Structures
A particular way to organize data so that it can be used efficiently
Interfaces
- Stack
- Queue
Stack
- A literal stack of data
- Push adds new data to top
- Pop removes data from the top
- Last item In First item Out - LIFO
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
Stack
Push
Pop
The LAST item IN would be the FIRST item OUT
ebay.com
devmountain.com
hackernoon.com
grabient.com
coolors.co
namecheap.com
Queue
- A literal line up of data
- Enqueue adds to back
- Dequeue removes from front
- First item In First item Out - FIFO
Managing multiple users who are looking to buy the last item in stock
Queue
Enqueue
Dequeue
The FIRST item IN would become the FIRST item OUT
Data Structures
- LinkedList
- Binary Tree
A particular way to organize data so that it can be used efficiently
Implementations
LinkedList
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
Binary Tree
"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
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.
Big O
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]
}
Big O
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)
Big O
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;
}
Big O
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.
Big O
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.
Speaking
Of
Sorting
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
Bubble Sort
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.
Insertion Sort
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.
Merge Sort
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.
Quick Sort
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
Recursion
Action, or function, calling itself until it has nothing left to call.
Recursion
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
Recursion
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
Copy of computer science
By jonmcd
Copy of computer science
- 157