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
/* Returns either the index of the location in the array, | |
or -1 if the array did not contain the targetValue */ | |
var doSearch = function(array, targetValue) { | |
var min = 0; | |
var max = array.length - 1; | |
var guess; | |
var i = 0; | |
while(min <= max) { | |
i = i + 1; | |
guess = Math.floor((min + max) / 2); |
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
var swap = function(array, firstIndex, secondIndex) { | |
var temp = array[firstIndex]; | |
array[firstIndex] = array[secondIndex]; | |
array[secondIndex] = temp; | |
}; | |
var indexOfMinimum = function(array, startIndex) { | |
var minValue = array[startIndex]; | |
var minIndex = startIndex; | |
for(var i = minIndex + 1; i < array.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
var insert = function(array, rightIndex, value) { | |
for(var j = rightIndex; | |
j >= 0 && array[j] > value; | |
j--) { | |
array[j + 1] = array[j]; | |
} | |
array[j + 1] = value; | |
}; | |
var insertionSort = function(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 quicksort(array) { | |
if (array.length <= 1) { | |
return array; | |
} | |
var pivot = array[0]; | |
var left = []; | |
var 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 quicksort(array) { | |
if (array.length <= 1) { | |
return array; | |
} | |
var pivot = array[0]; | |
var left = []; | |
var 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
// Split the array into halves and merge them recursively | |
function mergeSort (arr) { | |
if (arr.length === 1) { | |
// return once we hit an array with a single item | |
return arr | |
} | |
const middle = Math.floor(arr.length / 2) // get the middle item of the array rounded down | |
const left = arr.slice(0, middle) // items on the left side | |
const right = arr.slice(middle) // items on the right side |
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
// return index of element if element is present in the array, overwise return -1 | |
var linearSearch = function (element, array) { | |
for(var i = 0; i < array.length; i++) { | |
if(element === array[i]) { | |
return i; | |
} | |
} | |
return -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
var cons = function (x, y) { | |
return function(message) { | |
switch (message) { | |
case 'car': { | |
return x; | |
} | |
case 'cdr': { | |
return y; | |
} | |
} |
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
const cons = (x, y) => f => f(x, y); | |
const car = f => f((x, y) => x); | |
const cdr = f => f((x, y) => y); | |
function list() { | |
const h = Array.prototype.slice.call(arguments, 0)[0]; | |
const t = Array.prototype.slice.call(arguments, 1); | |
if (h == null) { | |
return cons(null, 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
class Queue { | |
constructor() { | |
this.items = []; | |
} | |
enqueue(obj) { | |
this.items.push(obj); | |
} | |
dequeue() { |
OlderNewer