Skip to content

Instantly share code, notes, and snippets.

@woonketwong
woonketwong / insertionSort
Created January 12, 2014 06:12
Insertion sort in javascript
var insertionSort = function(array){
var target;
var sortedIndex = 0;
var targetIndex;
for(var i = 0; i < array.length; i++){
target = array[i];
targetIndex = i;
for(var j = i; j >= sortedIndex; j--){
@woonketwong
woonketwong / mergeTwoSortedArray
Created December 31, 2013 18:58
Given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.
var mergeTwoSortedArray = function(array1, array2){
var array1IndexEnd = array1.length - array2.length - 1;
var arrayExtraIndexEnd = array1.length - 1;
var array2IndexEnd = array2.length - 1;
while( array2IndexEnd >= 0){
if ( array1[array1IndexEnd] > array2[array2IndexEnd]){
array1[arrayExtraIndexEnd] = array1[array1IndexEnd];
arrayExtraIndexEnd--;
array1IndexEnd--
@woonketwong
woonketwong / sortAndGroupAnagram
Created December 30, 2013 18:33
A method to sort an array of strings so that all the anagrams next to each other.
var sortAndGroupAnagram = function(array){
var result = {};
var sortedResult = [];
var currentKey;
var allKey;
for (var i = 0; i < array.length; i++){
currentKey = array[i].split('').sort().join('');
if ( result.hasOwnProperty(currentKey) ){
result[currentKey].push(array[i]);
@woonketwong
woonketwong / jugglingDBWithPostgres
Last active December 29, 2015 05:09
JugglingDB with Postgres
var q = require('q');
var Schema = require('jugglingdb').Schema;
var schema = new Schema('postgres', {
database: 'woonketwong',
username: 'woonketwong'
});
// The first argument to schema.define is the table
// and schema name. Do not use any capital letter
// in the table name because the database create table
@woonketwong
woonketwong / findLocalMin
Created February 19, 2014 00:35
Suppose we are given an array A[1 .. n] with the special property that A[1] ≥ A[2] and A[n − 1] ≤ A[n]. We say that an element A[x] is a local minimum if it is less than or equal to both its neighbors, or more formally, if A[x − 1] ≥ A[x] and A[x] ≤ A[x + 1]. For example, there are five local minima in the following array: 9 7 7 2 1 3 7 5 4 7 3 3…
function findLocalMin(array, start, end){
var mid = Math.floor( (start + end)/2 );
if (((mid - 1) < 0) || ((mid + 1) >= array.length)) return null;
if (array[mid - 2] > array[mid - 1] && array[mid - 1] < array[mid]){
return array[mid-1];
} else if ( (array[mid-1] >= array[mid-2])){
return findLocalMin(array, start, mid);
} else {
@woonketwong
woonketwong / breadthFirstSearch
Created February 18, 2014 14:42
Breadth first search.
function breadthFirstSearch(node){
// build a queue
var q = [];
// initialize q
q.push(node);
var currentNode = null;
@woonketwong
woonketwong / depthFirstSearch
Created February 18, 2014 14:32
Depth First Search
function depthFirstSearch(node){
if (node === null) return;
// visit node
console.log(node.value);
node.visited = true;
for (var i = 0; i < node.children.length; i++){
if (node.children[i].visited === false){
depthFirstSearch(node.children[i]);
@woonketwong
woonketwong / quickSort
Last active August 29, 2015 13:56
Quick sort.
function quickSort(array, start, end){
var leftIndex = partition(array, start, end);
if (start < leftIndex-1){
quickSort(array, start, leftIndex-1);
}
if (start > leftIndex){
quickSort(array, leftIndex, end);
@woonketwong
woonketwong / commonAncestor
Created February 18, 2014 05:16
Find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure.
function covers(root, node){
if (root === null ) return false;
if (root === node ) return true;
return covers(root.left, node) || covers(root.right, node);
}
function commonAncestorHelper(root, node1, node2){
if (root === null ) return false;
if (root === node1 || root === node2 ) return root;
@woonketwong
woonketwong / createMinimalBST
Created February 17, 2014 20:04
Given a sorted array, write an algorithm to create a binary search tree with minimal height.
function createMinimalBST(array, start, end){
if (end < start){
return null;
}
var mid = Math.floor( (start + end) / 2 );
var node = {val: array[mid], left: null, right: null};
node.left = createMinimalBST(array, start, mid-1);
node.right = createMinimalBST(array, mid+1, end);