Skip to content

Instantly share code, notes, and snippets.

View SparrowMike's full-sized avatar
🎯
Focusing

SparrowMike

🎯
Focusing
View GitHub Profile
@SparrowMike
SparrowMike / graph.js
Created November 20, 2022 15:41
basic graph
class Graph {
constructor() {
this.adjacencyList = {};
}
addVertex(vertex) {
if (!this.adjacencyList[vertex])
this.adjacencyList[vertex] = [];
}
// 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++) {
class MaxBinaryHeap {
constructor() {
this.values = [41, 39, 33, 18, 27, 12];
}
insert(val) {
this.values.push(val);
this.bubbleUp(val);
return this.values;
}
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
this.count = 0;
}
}
class BinarySearchTree {
class Node {
constructor(val) {
this.val = val;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
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
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]]
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;
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) {