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 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--){ |
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 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-- |
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 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]); |
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 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 |
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 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 { |
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 breadthFirstSearch(node){ | |
// build a queue | |
var q = []; | |
// initialize q | |
q.push(node); | |
var currentNode = 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
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]); |
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, start, end){ | |
var leftIndex = partition(array, start, end); | |
if (start < leftIndex-1){ | |
quickSort(array, start, leftIndex-1); | |
} | |
if (start > leftIndex){ | |
quickSort(array, leftIndex, end); |
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 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; | |
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 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); |