Skip to content

Instantly share code, notes, and snippets.

View liamgriffiths's full-sized avatar

Liam Griffiths liamgriffiths

View GitHub Profile
@liamgriffiths
liamgriffiths / blimpo.cc
Last active October 2, 2015 22:48
blimp code
/*
borrowed:
- IR code/library from http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
- ultrasonic sensor code from http://arduino.cc/en/Tutorial/Ping
*/
#include <IRremote.h>
#include <Servo.h>
// pins pins pins
@liamgriffiths
liamgriffiths / Graph.js
Last active July 28, 2019 20:39
graph. stack, and queue structures
function Graph(nodes) {
this.nodes = nodes || {};
}
Graph.prototype.print = function(storage, start, stop) {
start = start || 1;
storage.put(new Node(start));
while (! storage.isEmpty()) {
var seen = {};
// merge sort
// https://en.wikipedia.org/wiki/Merge_sort
var sort = function(list) {
// if list contains one element, it is sorted
if (list.length <= 1) return list;
// divide list in half into 2 sublists
var middle = Math.floor(list.length / 2);
var left = list.slice(0, middle);
// quick sort
// https://en.wikipedia.org/wiki/Quicksort
var sort = function(list) {
if (list.length <= 1) return list;
var pivot = list.splice(Math.floor(list.length / 2), 1);
var less = [], greater = [];
while (list.length) {
@liamgriffiths
liamgriffiths / set.js
Last active January 1, 2016 22:59
set data structure built on top of js object
var Set = function() {
this._items = {};
};
Set.prototype = {
values: function() {
return Object.keys(this._items);
},
insert: function(val) {
@liamgriffiths
liamgriffiths / binary_heap.js
Last active January 1, 2016 22:59
binary heap and priority queue structures
/*
* Binary Heap
*
* This is a heap which is built on top of a binary tree. The binary tree can
* be built using a regular array and some arithmetic to find the array indicies
* of a particular node's children.
*
* The Binary Heap is a 'complete tree', meaning that all levels of the tree are
* completely filled before going deeper. The levels of the tree are filled from
* left to right.
@liamgriffiths
liamgriffiths / bits.js
Last active January 1, 2016 22:59
bit shifting hacks
var bitwiseOR = function() {
// for each bit, if there is a "1" in the place, then the bit becomes "1"
var a = parseInt('00001111', 2);
var b = parseInt('11110000', 2);
var c = a | b;
c.toString(2); // '11111111'
// when the binary number is not the same length, 0's are added to the front
var d = parseInt('11110000', 2);
var e = parseInt('1100', 2); // becomes '00001100'
@liamgriffiths
liamgriffiths / echo_server.js
Last active April 28, 2022 09:14
simple node.js net tcp/unix socket echo server
/**
* echo server
*
*/
var net = require('net'),
port = 5000,
unixsocket = '/tmp/echo.sock';
var log = function(who, what) {
/**
* Binary Search Tree
*
* This is a tree data structure that orders the nodes of the tree such that:
* - The left subtree only contains nodes of lesser values
* - The right subtree only contains nodes of greater values
* - There are no duplicate nodes
*
* What this is good for:
* - Storing unique values

Big O notation (Asymptotic Notation)

The basic idea here is to give programmers a way to compare the performance of algorithms at a very high level in a language/platform/architecture independent way. This becomes particularly important when dealing with large inputs.

Big O notion may consider the worst-case, average-case, and best-case running time of an algorithm.

When describing an algorithm using Big O notation you will drop the lower-oder values. This is because when the inputs become very large, the lower-order values become far less important. For example:

Original Becomes