Skip to content

Instantly share code, notes, and snippets.

@woonketwong
woonketwong / selectionRank
Last active October 3, 2023 13:18
Selection Rank is a well known algorithm in computer science to find the ith smallest (or largest) element in an array in linear time. If the elements are unique, you can find the ith smallest or largest element in expected O(n) time. The code below implements finding the ith smallest element in javascript.
var selectionRank = function(array, rank){
// randomly select a pivot
var pivotIndex = Math.floor( (Math.random() * array.length) );
var pivot = array[pivotIndex];
// left array stores the smallest <rank> elements in the array
var leftArr = [];
var rightArr = [];
// partition into left and right arrays
for (var i = 0; i < array.length; i++){
@woonketwong
woonketwong / airteaching-local-gateway-setting.txt
Last active July 31, 2021 15:26
local airteaching gateway setting
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
@woonketwong
woonketwong / quickSort
Created January 10, 2014 19:40
Quick sort implementation in JavaScript.
var quickSort = function(array, left, right){
var leftIndex = partition(array, left, right);
if (left < leftIndex - 1){
quickSort(array, left, leftIndex-1);
}
if (right > leftIndex){
quickSort(array, leftIndex, right);
@woonketwong
woonketwong / findLocInInterspersedSortedArr
Created December 31, 2013 20:07
A binary search function on a sorted array of strings which is interspersed with empty strings.
var findLocInInterspersedSortedArr = function(strArr, target, first, last){
var mid = Math.floor( last - first / 2);
if (strArr[mid] === ''){
var left = mid - 1;
var right = mid + 1;
while (true){
if (left < first && right > last){
return -1;
@woonketwong
woonketwong / inOrderSucc
Created January 13, 2014 05:23
Write an algorithm to find the ‘next’ node (e g , in-order successor) of a given node in a binary search tree where each node has a link to its parent
var inOrderSucc = function(node){
var p = null;
if (node !== null){
if (node.parent === null || node.right !== null){
p = leftMostChild(node.right);
console.log("result=", p);
} else {
while ((p = node.parent) !== null){
if (p.left === node){
@woonketwong
woonketwong / findLevelLinkedList
Created January 13, 2014 03:09
Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists)
var findLevelLinkedList = function(root){
var queue = [];
var linkedListArray = [];
var level = 0;
queue.push([root, level]);
root.visit = true;
while(queue.length !== 0){
@woonketwong
woonketwong / createMinimalBST
Created January 12, 2014 20:13
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height
var createMinimalBST = function(array){
var makeNode = function(value){
var obj = {
value:value,
left:null,
right:null
};
return obj;
};
@woonketwong
woonketwong / searchNodeUsingDFS
Created January 12, 2014 19:09
Given a directed graph, design an algorithm to find out whether there is a route between two nodes using DFS.
var search = function(startNode, endNode){
//visit startNode
console.log("Visting:", startNode.key);
if (startNode.key === endNode.key){
return true;
}
startNode.visit = true;
@woonketwong
woonketwong / isBalanced
Last active January 3, 2016 01:19
Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one.
var isBalanced = function(node){
var maxDepth = function(node){
if (node === undefined){
return 0;
}
return 1 + Math.max(maxDepth(node.next[0]), maxDepth(node.next[1]));
}
var minDepth = function(node){
@woonketwong
woonketwong / breathFirstSearch
Created January 12, 2014 18:01
Breath first search implementation with a queue.
var breathFirstSearch = function(node){
var queue = [];
// visit root
console.log("Node key, value", node.key, node.value);
root.visit=true;
queue.push(root);
while(queue.length !== 0){
currentNode = queue.pop();