Skip to content

Instantly share code, notes, and snippets.

View bluurn's full-sized avatar
💭
My other car is cdr

Vladimir Suvorov bluurn

💭
My other car is cdr
  • Xempus AG
  • Berlin
View GitHub Profile
@bluurn
bluurn / binary_search.js
Last active January 29, 2019 14:01
JS: Binary Search implementation
/* 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);
@bluurn
bluurn / selection_sort.js
Created January 29, 2019 14:03
JS: Selection Sort Implementation
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++) {
@bluurn
bluurn / insertion_sort.js
Created January 29, 2019 14:05
JS: Implement Insertion Sort
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) {
@bluurn
bluurn / quick_sort.js
Created January 29, 2019 14:07
JS: Implement Quick Sort
function quicksort(array) {
if (array.length <= 1) {
return array;
}
var pivot = array[0];
var left = [];
var right = [];
@bluurn
bluurn / quick_sort.js
Created January 29, 2019 14:07
JS: Implement Quick Sort
function quicksort(array) {
if (array.length <= 1) {
return array;
}
var pivot = array[0];
var left = [];
var right = [];
@bluurn
bluurn / merge_sort.js
Created January 29, 2019 14:08
JS: Implement Merge Sort
// 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
@bluurn
bluurn / linear_search.js
Created January 29, 2019 14:17
JS: Linear Search Implementation
// 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;
}
@bluurn
bluurn / pair.js
Created January 29, 2019 14:25
JS: Pair Implementation
var cons = function (x, y) {
return function(message) {
switch (message) {
case 'car': {
return x;
}
case 'cdr': {
return y;
}
}
@bluurn
bluurn / lists.js
Created January 29, 2019 14:29
JS: List Implementation
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);
}
@bluurn
bluurn / queue.js
Last active January 30, 2019 08:51
JS: Queue Implementation
class Queue {
constructor() {
this.items = [];
}
enqueue(obj) {
this.items.push(obj);
}
dequeue() {