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 Graph { | |
constructor() { | |
this.adjacencyList = {}; | |
} | |
addVertex(vertex) { | |
if (!this.adjacencyList[vertex]) | |
this.adjacencyList[vertex] = []; | |
} |
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
// Seperate Chaining | |
class Hashtable { | |
constructor(size = 53) { | |
this.keyMap = new Array(size) | |
} | |
_hash(key) { | |
let total = 0; | |
let WEIRD_PRIME = 31; | |
for (let i = 0; i < Math.min(key.length, 100); 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
class MaxBinaryHeap { | |
constructor() { | |
this.values = [41, 39, 33, 18, 27, 12]; | |
} | |
insert(val) { | |
this.values.push(val); | |
this.bubbleUp(val); | |
return this.values; | |
} |
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 Node { | |
constructor(value) { | |
this.value = value; | |
this.left = null; | |
this.right = null; | |
this.count = 0; | |
} | |
} | |
class BinarySearchTree { |
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 Node { | |
constructor(val) { | |
this.val = val; | |
this.next = null; | |
this.prev = null; | |
} | |
} | |
class DoublyLinkedList { | |
constructor() { |
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 Node { | |
constructor(val) { | |
this.val = val; | |
this.next = null; | |
} | |
} | |
class SinglyLinkedList { | |
constructor() { | |
this.head = 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
function swap(arr, idx1, idx2) { | |
let temp = arr[idx1]; | |
arr[idx1] = arr[idx2]; | |
arr[idx2] = temp; | |
// [arr[idx1], arr[idx2]] = [arr[idx2], arr[idx1]] | |
} | |
function bubbleSort(arr) { | |
let noSwap |
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
function selectionSort(arr) { | |
for(let i = 0; i < arr.length; i++) { | |
let smallest = i | |
for(let j = i + 1; j < arr.length; j++) { | |
if (arr[j] < arr[smallest]) { | |
smallest = j | |
} | |
} | |
if (smallest !== i) { | |
[arr[i], arr[smallest]] = [arr[smallest], arr[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
function insertionSort1(arr){ | |
var currentVal; | |
for(var i = 1; i < arr.length; i++){ | |
currentVal = arr[i]; | |
for(var j = i - 1; j >= 0 && arr[j] > currentVal; j--) { | |
arr[j+1] = arr[j] | |
} | |
arr[j+1] = currentVal; | |
} | |
return arr; |
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
function getDigit(num, i) { | |
return Math.floor(Math.abs(num) / Math.pow(10, i)) % 10; | |
} | |
function digitCount(num) { | |
if (num === 0) return 1; | |
return Math.floor(Math.log10(Math.abs(num))) + 1; | |
} | |
function mostDigits(nums) { |
NewerOlder