You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calculating the steps to get back to 1:
The Collatz conjecture applies to positive numbers and speculates that is alway possible to get back to 1 if you follow these steps:
If n is 1, stop.
Otherwise, if n is even, repeate this process on n/2.
Otherise, if n is odd, repeat this process on 3n + 1.
Write a recursive function that calculatees how many steps it takes to get to 1:
BubbleSort and SelectionSort take significantly more time to process each time the dataset gets larger.
BubbleSort and SelectionSort are perfectly fine to use on smaller datasets that will remain small.
MergeSort is much better suited for large datasets, but is a little more complex to implement.
Sorting Algorithm
Runtime (Big O Notation)
BubbleSort
n^2
SelectionSort
n^2
MergeSort
n * log(n)
Bubble Sort
Runtime: n^2
'bubbles' the largest elements to the end
For each element, bubbleSort has an inner loop that also loops through each element until array.length - i (gets smaller each outer iteration), it then checks that 'j' item against 'j + 1' and if 'j' is larger they swap. It continues this process until the largest 'j' item is 'bubbled' to the end. This process continues until 'i' reaches array.length-1 unless checks are in place to see if it has already been sorted.
Steps:
Loop through each element in the array (outer loop)
For each element, loop through each element again to array.length - i (i increases, so the j iteration becomes smaller for each i iteration)
if the element j is greater than j + 1, swap the elements
functionbubbleSort(arr){letswapped;for(leti=0;i<arr.length-1;i++){if(swapped===false){returnarr;}console.log('running the loop!');swapped=false;for(letj=0;j<arr.length-1-i;j++){if(arr[j]>arr[j+1]){[arr[j],arr[j+1]]=[arr[j+1],arr[j]];swapped=true;}}}returnarr;}
Selection Sort
Runtime: n^2
For each element, selectionSort assumes that element is the min, it then iterates through the rest of the array and compares each value to the element, if any of these values are less than the original element, they are swapped
Nickname: prove me wrong algorithm
Steps:
Iterate through each array item
Assume the element at 'i' is the least in the array, assign 'i' to 'indexOfMin'
For j from i + 1 to end of array
See if there is an element with a lower value than i
If there is, record its index
If the index of the current element and the index of the 'lowest' element is not the same, swap them
Splits the array in half and then calls MergeSort recursively
Merge sort uses two functions:
MergeSort
Merge (called within MergeSort, can sort and merge 2 sorted arrays)
Merge Steps:
Create 'results' array
While there are still elements in both arrays:
If the first element (index 0) in the left half is less than the first than the first element (index 0) in the right half
* 'shift' the element from left into a 'result' array
else
* 'shift' the element from right into a 'result' array
Take everything from the array that still has stuff in it and put it in 'results'
MergeSort function steps:
Check base case: Array.length === 1.
Find the center of the array: Math.floor(arr.length / 2)
Split the array into left and right using .slice
return merge while passing in (mergeSort(left), mergeSort(right)) as the params
shift the first item off the array and put it on the second array
keep shifting numbers off the original array, save to variable, compare then set new index
compare the shift number to the last item in the new array, if smaller swap, bubble the number down until it is greater than the previous number (reverse for loop)
Quick Sort
pick a pivot point and pop off the array (usually the last item anyway)
create two new arrays: one less than the pivot and one greater than the pivot
keep poping off numbers from the original and compare to the pivot, if greater push into greater array, if less than push into less than array.
can now assume that the pivot is in its correct index
recursively call quickSort on the lessThanArray and the greaterThanArray
Steps:
Establish a base case, if array.length <= 1 return that array
use array.pop() to take the last element of the array and save it as the pivot
create two empty arrays named lessThan or greaterThan
for loop to iterate over the original array, if the currentElement is less than pivot, push the element into lessThan, else push the element into greaterThan
recursively call quickSort on the lessThan array and the greaterThan array
parent child relationship, top parent is called the root, last child is called a leaf.
particularly good for inserting, finding and moving around data in an efficient, cheap way.
rules:
Each BST has a root node, which contains data and has no parent nodes.
Each node has zero to two, child nodes (word binary comes from max 2 child nodes)
Each child node linked to the left has a data value less than or equal to the parent node.
Each child node linked to the right has a data value greater than the parent node.
Ideally you want to avoid duplicates, conditional logic in nodes to catch duplicates, most data being entered in BST's will be unique by default
Insert sudo code:
class Binary Search Tree
root node = null;
class Node
data = value;
left = null;
right = null;
Check the node (default to root)
Compare to node
check if the currNode has children
if true, move down and repeat process for next node, else set as child to currNode
Find sudo code:
check the root
compare
if less than check left child, if greater than check right child
repeat
Traversing the trie: Depth First Search vs. Breadth First Search
both vising every node in the tree
DFS
BFS
Finish all the nodes (including the grandChildren)
Visit all nodes at the same depth (children before grandChildren)
PreOrder, InOrder, PostOrder
by Level
Stack
First In, Last Out
2 methods: Push() & Pop()
Higher Order Functions
A function that takes another function (callback) as an argument, and/or, returns a function.
examples: .then, .map, .filter, .reduce
constadd=(num1,num2)=>{returnnum1+num2}// takes in a functionconstcompute=(operation,num1,num2)=>{returnoperation(num1,num2)}// returns a functionconstcomputer=operation=>{return(num1,num2)=operation(num1,num2)}
Currying
decomposing a function with multiple arguments into a chain of functions each with only one argument