This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const swap = require('./swap'); | |
const quicksort = (array, leftBound = 0, rightBound = array.length - 1) => { | |
if (leftBound < rightBound) { | |
console.log('. Calling partition', array, `with leftBound ${leftBound} and rightBound ${rightBound}`); | |
const pivotIndex = partition(array, leftBound, rightBound); | |
console.log(`. Returning pivotIndex = ${pivotIndex}`); | |
console.log(`\nCalling quicksort for left partition with leftBound ${leftBound} and (pivotIndex-1) ${pivotIndex - 1}`); | |
quicksort(array, leftBound, pivotIndex - 1); | |
console.log(`\nCalling quicksort for right partition with pivotIndex ${pivotIndex} and rightBound ${rightBound}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mergeSort = (startArray) => { | |
const length = startArray.length; | |
if (length === 1) { | |
return startArray; | |
} | |
const mid = Math.floor(length / 2); | |
const leftArray = startArray.slice(0, mid); | |
const rightArray = startArray.slice(mid, length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const swap = require("./swap"); | |
const bubbleSort = (input) => { | |
let swapCount = 0; | |
let swapping = true; | |
while (swapping) { | |
swapping = false; | |
for (let i = 0; i < input.length - 1; i++) { | |
if (input[i] > input[i + 1]) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Node = require("./Node"); | |
class LinkedList { | |
constructor() { | |
this.head = null; | |
} | |
addToHead(data) { | |
const nextNode = new Node(data); | |
const currentHead = this.head; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Edge { | |
constructor(start, end, weight = null) { | |
this.start = start; | |
this.end = end; | |
this.weight = weight; | |
} | |
} | |
module.exports = Edge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MinHeap { | |
constructor() { | |
this.heap = [null]; | |
this.size = 0; | |
} | |
popMin() { | |
if (this.size === 0) { | |
return null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const LinkedList = require("./LinkedList"); | |
const Node = require("./Node"); | |
class HashMap { | |
constructor(size = 0) { | |
this.hashmap = new Array(size).fill(null).map(() => new LinkedList()); | |
} | |
hash(key) { | |
let hashCode = 0; | |
for (let i = 0; i < key.length; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hash map: A key-value store that uses an array and a hashing function to save and retrieve values. | |
Key: The identifier given to a value for later retrieval. | |
Hash function: A function that takes some input and returns a number. | |
Compression function: A function that transforms its inputs into some smaller range of possible outputs. | |
Recipe for saving to a hash table: | |
- Take the key and plug it into the hash function, getting the hash code. | |
- Modulo that hash code by the length of the underlying array, getting an array index. | |
- Check if the array at that index is empty, if so, save the value (and the key) there. | |
- If the array is full at that index continue to the next possible position depending on your collision strategy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Node = require('./Node'); | |
class LinkedList { | |
constructor() { | |
this.head = null; | |
} | |
addToHead(data) { | |
const newHead = new Node(data); | |
const currentHead = this.head; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Node = require('./Node'); | |
class LinkedList { | |
constructor() { | |
this.head = null; | |
} | |
addToHead(value) { | |
const nextNode = new Node(value); | |
const currentHead = this.head; |