Skip to content

Instantly share code, notes, and snippets.

function bubbleSort(arr) {
let swapped = true;
while(swapped){
swapped = false;
for(let j = 0; j < arr.length - 1; j++){
if(arr[j] > arr[j+1]){
swapped = true;
@jharris-code
jharris-code / binary_search.js
Created January 24, 2019 01:36
Binary Search
function binarySearch(nums, target) {
let start = 0;
let end = nums.length - 1;
let p;
while(start <= end) {
p = Math.floor((start + end) / 2);
if(nums[p] === target) {
return p;
@jharris-code
jharris-code / depth_first_search.js
Created January 14, 2019 19:57
Depth First Search
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
//Time Complexity: O(N) since we traverse every node worst case
//Space Complexity: O(H) height of tree is number of stack frames in memory
@jharris-code
jharris-code / breadth_first_search.js
Last active January 14, 2019 19:56
Breadth First Search
//Assumes:
//a Queue data structure is available with push(), pop(), isEmpty() methods.
//each node in the Queue have left and right properties.
//Time Complexity: O(N)
//Space Complexity: O(N)
const bfs = (root, value) => {
let q = new Queue();
q.push(root);
@jharris-code
jharris-code / merge_sort.js
Last active January 24, 2019 23:17
Merge Sort
//Time Complexity: O(N log N) - mergeSort is called Log N times, each merge iterates over N elements, so N*(Log N)
//Space Complexity: O(N) - the depth of the call stack is Log N, and each recursive call contains a N/2 copy of the array.
//If you sum up each sub array in the call stack this would be N/2 + N/4 + N/8 + 1 which equals N.
//If new sub arrays were NOT created in each call, then space requirement would be only for the stack frames, which would be Log N.
const mergeSort = (a) => {
if(a.length === 1) {
return a;
}
let middle = parseInt(a.length / 2);
let end = a.length;
@jharris-code
jharris-code / quick_sort.js
Created January 10, 2019 18:51
Quick Sort
//Time Complexity: Worst: O(N^2) - if pivot point is most extreme, and array is already fully sorted,
//then complexity of partition will be N, then N - 1, then N - 2, etc. Sum up all the N's and this results in N^2
//Average/Best case: O(N Log N) - if pivot point is at the middle then N + (N/2) + (N/4) + (N/8)... = N * (Log N)
//Space Complexity: O(log N) - array is sorted in place, however, stack frames result in Log N recursive calls
let arr = [5,1,9,10,15,7,7,12];
//Time Complexity O(N)
const partition = (low, high) => {
@jharris-code
jharris-code / heap_sort.js
Last active January 22, 2019 20:54
Heap Sort
//Create a sorted array (ascending) from a max heap
//Time Complexity: O(N log N)
//Space Complexity: O(1) because array is sorted in place
const heapSort = (arr, end) => {
while(end > 0){
let tmp = arr[end];
arr[end] = arr[0];
arr[0] = tmp;
end -= 1;
@jharris-code
jharris-code / heapify_array.js
Last active January 10, 2019 18:56
Heapify Array
//converting an array to a heap is performed with two functions, heapify and siftDown.
//The result is an array representation of a max heap.
//A max heap is required to perform a heap sort.
//Time Complexity: O(N). Although siftDown is O(log n), the overall runtime is O(N).
//Good info here on time complexity: https://www.growingwiththeweb.com/data-structures/binary-heap/build-heap-proof/
//Space Complexity: O(1) auxiliary space is 0 since the array is converted to a heap in place.
const heapify = (arr) => {
//end is the last element of the array
@jharris-code
jharris-code / insertion_sort.js
Created January 8, 2019 23:38
JavaScript Insertion Sort
let a = [3,1,2,15,4,3,8,8,0];
let c = 0;
//Time Complexity: O(N^2)
//Space Complexity: O(1) because auxiliary space required (above original data set) is 0.
const insertionSort = (arr) => {
for(let i=0; i < arr.length; i++){
let j = i;
while(j > 0 && arr[j] < arr[j-1]) {
@jharris-code
jharris-code / graph_adjacency_matrix.js
Last active January 8, 2019 20:51
Graph Directed Adjacency Matrix
//Storage: O(V^2)
class Graph {
//O(V^2)
constructor(size){
this.vertices = [];
for(let i = 0; i < size; i++){
this.vertices.push([]);
for(let j = 0; j < size; j++){
this.vertices[i].push(false);