Skip to content

Instantly share code, notes, and snippets.

View abenteuerzeit's full-sized avatar
💭
Open to collaborate

Adrian Mróz abenteuerzeit

💭
Open to collaborate
View GitHub Profile
@abenteuerzeit
abenteuerzeit / quicksort.js
Last active August 17, 2023 15:49 — forked from codecademydev/quicksort.js
Quicksort
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}`);
@abenteuerzeit
abenteuerzeit / index.js
Created August 17, 2023 13:21 — forked from codecademydev/index.js
Merge Sort
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);
@abenteuerzeit
abenteuerzeit / bubbleSort.js
Created August 17, 2023 12:45 — forked from codecademydev/bubbleSort.js
Bubble sort
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]) {
@abenteuerzeit
abenteuerzeit / LinkedList.js
Last active August 15, 2023 17:33 — forked from codecademydev/LinkedList.js
Recursion
const Node = require("./Node");
class LinkedList {
constructor() {
this.head = null;
}
addToHead(data) {
const nextNode = new Node(data);
const currentHead = this.head;
@abenteuerzeit
abenteuerzeit / Edge.js
Created August 15, 2023 11:14 — forked from codecademydev/Edge.js
Graph
class Edge {
constructor(start, end, weight = null) {
this.start = start;
this.end = end;
this.weight = weight;
}
}
module.exports = Edge;
@abenteuerzeit
abenteuerzeit / MinHeap.js
Last active August 15, 2023 09:15 — forked from codecademydev/MinHeap.js
Min-Heap
class MinHeap {
constructor() {
this.heap = [null];
this.size = 0;
}
popMin() {
if (this.size === 0) {
return null;
}
@abenteuerzeit
abenteuerzeit / HashMap.js
Created August 14, 2023 17:26 — forked from codecademydev/HashMap.js
Codecademy export
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++) {
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.
@abenteuerzeit
abenteuerzeit / LinkedList.js
Last active August 13, 2023 10:41 — forked from codecademydev/LinkedList.js
Web Navigator
const Node = require('./Node');
class LinkedList {
constructor() {
this.head = null;
}
addToHead(data) {
const newHead = new Node(data);
const currentHead = this.head;
@abenteuerzeit
abenteuerzeit / LinkedList.js
Last active August 13, 2023 10:09 — forked from codecademydev/LinkedList.js
Data Structure: Stack
const Node = require('./Node');
class LinkedList {
constructor() {
this.head = null;
}
addToHead(value) {
const nextNode = new Node(value);
const currentHead = this.head;