Skip to content

Instantly share code, notes, and snippets.

View galenweber's full-sized avatar

Galen Weber galenweber

  • New York Federal Reserve
  • New York City
View GitHub Profile
// 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;
}
// 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 {
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;
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);
}
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];
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];
var mergeSort = function(array) {
array = genArray(array);
return sort(array);
}
var merge = function(arrayOne, arrayTwo) {
// 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,
class Stack {
constructor() {
this.body = {};
this.minimum;
}
push(elem) {
this.body = {
val: elem,
below: Object.assign({}, this.body)
class Queue {
constructor() {
this.body = {};
this.minimum;
}
enqueue(val) {
const recurse = function recurse(obj) {
if (obj.above === undefined) {
obj.val = val;