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
// Given an array of integers, find a peak if it exists | |
// Where a peak is defined as greater than or equal to previous | |
// and subsequent values (for edge, >= to sole neighbor) | |
var findPeak = function(array) { | |
for (var i=1; i<array.length; i++) { | |
if ((array[i] >= array[i-1]) && (array[i] >= array[i+1])) { | |
return 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
// Given an array of integers, find a peak if it exists | |
// Where a peak is defined as greater than or equal to previous | |
// and subsequent values (for edge, >= to sole neighbor) | |
var isPeak = function(l,m,r) { | |
if (typeof l === 'undefined') { | |
return (m>=r) | |
} else if (typeof r === 'undefined') { | |
return (m>=l); | |
} else { |
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 bubbleSort = function(array) { | |
var madeSort = false; | |
for (var i=1; i < array.length; i++) { | |
if (array[i] < array[i-1]) { | |
var temp = array[i]; | |
array[i] = array[i-1]; | |
array[i-1] = temp; | |
madeSort = true; |
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 insertionSortwSplice = function(array) { | |
for (var i = 1; i < array.length; i++) { | |
for (var x = 0; x < i; x++ ) { | |
if (array[i] < array[x]) { | |
// Calling splice twice has got to be inefficient | |
array.splice(x,0,array[i]); | |
array.splice(i+1,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 insertionSort = function(array) { | |
var temp; | |
for (var i = 1; i < array.length; i++) { | |
for (var x = 0; x < i; x++ ) { | |
if (array[i] < array[x]) { | |
temp = array[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 insertionSort = function(array) { | |
var temp; | |
for (var i = 1; i < array.length; i++) { | |
temp = i; | |
for (var x = i-1; x >= 0 && array[x] > i; x-- ) { | |
array[x+1] = array[x]; |
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 mergeSort = function(array) { | |
array = genArray(array); | |
return sort(array); | |
} | |
var merge = function(arrayOne, arrayTwo) { |
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
// Binary search tree | |
// Functions to insert, generate, and count nodes | |
// Employs a functional approach | |
var insert = function(value, tree) { | |
// First check whether there is a head | |
// If not, set value as head | |
if (!tree.value) { | |
var tree = { | |
value: value, |
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 Stack { | |
constructor() { | |
this.body = {}; | |
this.minimum; | |
} | |
push(elem) { | |
this.body = { | |
val: elem, | |
below: Object.assign({}, this.body) |
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.body = {}; | |
this.minimum; | |
} | |
enqueue(val) { | |
const recurse = function recurse(obj) { | |
if (obj.above === undefined) { | |
obj.val = val; |
OlderNewer