Created
August 15, 2016 21:38
-
-
Save jeffsheets/07516e90fe8c9e9c5b825e067c408aff to your computer and use it in GitHub Desktop.
Javascript binary search, quick sort, depth first search, and breadth first search
This file contains hidden or 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
//See full source with HTML at jsfiddle: | |
// https://jsfiddle.net/jeffsheets/ry2ns0s7/4/ | |
function node(val, left, right) { | |
return {val: val, left: left, right: right, visited: false}; | |
} | |
/* | |
1 | |
2 3 | |
4 5 6 7 | |
8 9 | |
*/ | |
function tree() { | |
return node('1', node('2', node('4', node('8')), node('5')), node('3', node('6'), node('7', null, node('9')))); | |
} | |
function bfs(node, result=[]) { | |
const queue = []; | |
queue.push(node); | |
while (queue.length > 0) { | |
const it = queue.shift(); | |
result.push(it.val); | |
if (it.left) { | |
queue.push(it.left); | |
} | |
if (it.right) { | |
queue.push(it.right); | |
} | |
} | |
return result; | |
} | |
function dfs_stack_inorder(node, result=[]) { | |
const stack = []; | |
stack.push(node); | |
while (stack.length > 0) { | |
const it = stack.pop(); | |
if (!it.visited) { | |
it.visited = true; | |
if (it.right) { | |
stack.push(it.right); | |
} | |
stack.push(it); | |
if (it.left) { | |
stack.push(it.left); | |
} | |
} else { | |
result.push(it.val); | |
} | |
} | |
return result; | |
} | |
function dfs_stack_preorder(node, result=[]) { | |
const stack = []; | |
stack.push(node); | |
while (stack.length > 0) { | |
const it = stack.pop(); | |
if (!it.visited) { | |
it.visited = true; | |
if (it.right) { | |
stack.push(it.right); | |
} | |
if (it.left) { | |
stack.push(it.left); | |
} | |
stack.push(it); | |
} else { | |
result.push(it.val); | |
} | |
} | |
return result; | |
} | |
function dfs_stack_postorder(node, result=[]) { | |
const stack = []; | |
stack.push(node); | |
while (stack.length > 0) { | |
const it = stack.pop(); | |
if (!it.visited) { | |
it.visited = true; | |
stack.push(it); | |
if (it.right) { | |
stack.push(it.right); | |
} | |
if (it.left) { | |
stack.push(it.left); | |
} | |
} else { | |
result.push(it.val); | |
} | |
} | |
return result; | |
} | |
function dfs_inorder(node, result=[]) { | |
if (node.left) { | |
dfs_inorder(node.left, result); | |
} | |
result.push(node.val); | |
if (node.right) { | |
dfs_inorder(node.right, result); | |
} | |
return result; | |
} | |
function dfs_preorder(node, result=[]) { | |
result.push(node.val); | |
if (node.left) { | |
dfs_preorder(node.left, result); | |
} | |
if (node.right) { | |
dfs_preorder(node.right, result); | |
} | |
return result; | |
} | |
function dfs_postorder(node, result=[]) { | |
if (node.left) { | |
dfs_postorder(node.left, result); | |
} | |
if (node.right) { | |
dfs_postorder(node.right, result); | |
} | |
result.push(node.val); | |
return result; | |
} | |
function binarySearch(toFind, list) { | |
let low = 0; | |
let high = list.length - 1; | |
while (low <= high) { | |
let middle = Math.floor((low + high) / 2); | |
if (list[middle] === toFind) { | |
return middle; | |
} | |
if (list[middle] < toFind) { | |
low = middle + 1; | |
} else { | |
high = middle - 1; | |
} | |
} | |
return -1; //not found | |
} | |
function quickSort(list) { | |
if (list.length <= 1) { | |
return list; | |
} | |
const middleIndex = Math.floor(list.length / 2); | |
const middle = list[middleIndex]; | |
const left = []; | |
const right = []; | |
for (let i = 0; i < list.length; i++) { | |
if (i !== middleIndex) { | |
if (list[i] <= middle) | |
left.push(list[i]); | |
else | |
right.push(list[i]); | |
} | |
} | |
const listOfLists = quickSort(left); | |
listOfLists.push([middle]); | |
listOfLists.push(quickSort(right)); | |
return [].concat.apply([], listOfLists); | |
} | |
document.getElementById('bfs').innerText = bfs(tree()); | |
document.getElementById('dfs_stack_inorder').innerText = dfs_stack_inorder(tree()); | |
document.getElementById('dfs_stack_preorder').innerText = dfs_stack_preorder(tree()); | |
document.getElementById('dfs_stack_postorder').innerText = dfs_stack_postorder(tree()); | |
document.getElementById('dfs_inorder').innerText = dfs_inorder(tree()); | |
document.getElementById('dfs_preorder').innerText = dfs_preorder(tree()); | |
document.getElementById('dfs_postorder').innerText = dfs_postorder(tree()); | |
const primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83]; | |
document.getElementById('bs-0').innerText = binarySearch(67, primes); | |
document.getElementById('bs-1').innerText = binarySearch(8, primes); | |
document.getElementById('bs-2').innerText = binarySearch(2, primes); | |
document.getElementById('bs-3').innerText = binarySearch(83, primes); | |
document.getElementById('bs-4').innerText = binarySearch(89, primes); | |
const list = [4, 6, 1, 2, 8, 9, 3, 0, 10, 3] | |
document.getElementById('quickSort').innerText = quickSort(list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment