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
/** | |
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) { |
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 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; |
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 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; | |
} |
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 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++) { |
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 arrayConcatenating(...arrs) { | |
let newArr = []; | |
for (let i = 0; i < arrs.length; i++) { | |
newArr = newArr.concat(arrs[i]); | |
} | |
return newArr; | |
} |
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
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 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
// This is how tree should look like | |
// const tree = { | |
// root: { | |
// left: { | |
// left: { | |
// left: null, | |
// right: null, | |
// value: 3 | |
// }, | |
// right: { |
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 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; | |
} |
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 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], | |
); |
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 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]); | |
} |
OlderNewer