Skip to content

Instantly share code, notes, and snippets.

View ungarson's full-sized avatar
🎯
Focusing

Yo ungarson

🎯
Focusing
View GitHub Profile
@ungarson
ungarson / mergesort.js
Created February 5, 2019 13:29
Merge sort in javascript
/**
Imagine we have [1, 2] and [3,4]
So using merge_union we will concatenate these arrays and sort them right
merge_union([2,1], [3,4]) ---> [1,2,3,4]
merge_union([2,3,4,5], [-5,-3,100]) ---> [-5, -3, 2, 3, 4, 5, 100]
**/
function merge_union(arr1, arr2) {
@ungarson
ungarson / quicksort.js
Created February 5, 2019 13:29
Quick sort in javascript
function arr_union(arr1, arr2) {
return arr1.concat(arr2);
}
function quicksort(array) {
let pivot = 0;
let compared = array.length - 1;
if (array.length <= 1) return array;
@ungarson
ungarson / heapsort.js
Created March 20, 2019 08:25
Heap sort in js
function maxHeapify(arr, i) {
const l = i * 2 + 1;
const r = i * 2 + 2;
let largest;
if (l <= arr.heapSize && arr[l] > arr[i]) {
largest = l;
} else largest = i;
if (r <= arr.heapSize && arr[r] > arr[largest]) {
largest = r;
}
@ungarson
ungarson / countingSort.js
Created April 5, 2019 07:00
Counting sort in javascript
function countingSort(arr, maxNumber) {
let tempArr = new Array(maxNumber + 1).fill(0);
let returnArr = [];
for (let i = 0; i < arr.length; i++) {
tempArr[arr[i]] += 1;
}
for (let i = 1; i < tempArr.length; i++) {
@ungarson
ungarson / radixSort.js
Created April 6, 2019 06:18
Radix sort in javascript
function arrayConcatenating(...arrs) {
let newArr = [];
for (let i = 0; i < arrs.length; i++) {
newArr = newArr.concat(arrs[i]);
}
return newArr;
}
@ungarson
ungarson / bucketSort.js
Last active April 6, 2019 06:59
Bucket sort in javascript
let insertionSort = (inputArr) => {
let length = inputArr.length;
for (let i = 1; i < length; i++) {
let key = inputArr[i];
let j = i - 1;
while (j >= 0 && inputArr[j] > key) {
inputArr[j + 1] = inputArr[j];
j = j - 1;
}
inputArr[j + 1] = key;
// This is how tree should look like
// const tree = {
// root: {
// left: {
// left: {
// left: null,
// right: null,
// value: 3
// },
// right: {
@ungarson
ungarson / fibonacciMemo.js
Last active June 12, 2019 05:19
Fibonacci with memoization
function fib(pos, fibValues) {
if (typeof fibValues[pos] !== "undefined") {
return fibValues[pos];
}
result = fib(pos - 1, fibValues) + fib(pos - 2, fibValues);
fibValues[pos] = result;
return result;
}
@ungarson
ungarson / longestCommonSubsequence.js
Last active May 12, 2019 09:37
Finding a Longest Common Subsequence of two strings in javascript
function forwardTrace(lcsMatrix, set1, set2) {
for (let rowIndex = 1; rowIndex <= set2.length; rowIndex += 1) {
for (let columnIndex = 1; columnIndex <= set1.length; columnIndex += 1) {
if (set1[columnIndex - 1] === set2[rowIndex - 1]) {
lcsMatrix[rowIndex][columnIndex] = lcsMatrix[rowIndex - 1][columnIndex - 1] + 1;
} else {
lcsMatrix[rowIndex][columnIndex] = Math.max(
lcsMatrix[rowIndex - 1][columnIndex],
lcsMatrix[rowIndex][columnIndex - 1],
);
@ungarson
ungarson / ifThereIsRoute.js
Created June 9, 2019 06:02
A function that tells you if there is a nested object with specific name
function ifThereIsRouteFrom(start) {
const queue = [start];
return {
to(finish) {
currentCity = queue.shift();
if (typeof currentCity === "undefined") return false;
for (let i = 0, len = currentCity.length; i < len; i++) {
if (currentCity[i]['name'] === finish) return true;
queue.push(currentCity[i]);
}