Skip to content

Instantly share code, notes, and snippets.

View KerryJones's full-sized avatar

Kerry Jones KerryJones

View GitHub Profile
@KerryJones
KerryJones / .gitconfig
Last active August 29, 2015 14:23
Aliases
[alias]
brr = branch -r
br = branch
ci = commit -m
cia = commit -a -m
amend = commit --amend -C HEAD
co = checkout
cob = checkout -b
st = status -sb
me = merge --no-ff
@KerryJones
KerryJones / selection-sort.php
Created November 30, 2015 03:50
PHP Selection Sort O(n^2)
<?php
// O(n^2)
function selectionSort( array $array ) {
$length = count($array);
for ( $i = 0; $i < $length; $i++) {
$min = $i;
for( $j = $i + 1; $j < $length; $j++ ) {
if ( $array[$min] > $array[$j] )
@KerryJones
KerryJones / insertion-sort.php
Last active November 30, 2015 04:27
PHP Insertion Sort O(n^2)
<?php
// O(n^2)
function insertionSort( array $array ) {
$length = count($array);
for ( $i = 1; $i < $length; $i++ ) {
$j = $i;
while ( $j > 0 && $array[$j] < $array[$j-1] ) {
$temp = $array[$j];
$array[$j] = $array[$j-1];
$array[$j-1] = $temp;
@KerryJones
KerryJones / merge-sort.php
Last active November 30, 2015 05:03
PHP Merge Sort O(n log n)
<?php
function mergeSort(array $array) {
$length = count( $array );
// An array with one value is sorted
if ( $length < 2 )
return $array;
// Split array in half
$left = $right = [];
@KerryJones
KerryJones / data-structure-stack.php
Last active December 4, 2015 06:47
PHP Data Structure: Stack
class Stack {
protected $top;
public function push($data) {
$node = new Node($data);
$node->next = $this->top;
$this->top = $node;
}
@KerryJones
KerryJones / data-structure-linked-list.php
Last active December 4, 2015 06:47
PHP Data Structure: Linked List
class LinkedList {
public $head;
public function add($data) {
$new_node = new Node($data);
if ( is_null( $this->head ) ) {
$this->head = $new_node;
} else
{
@KerryJones
KerryJones / data-structure-binary-search-tree.php
Last active December 4, 2015 06:48
PHP Data Structure: Binary Search Tree (BST)
<?php
class BST {
protected $root;
/**
* Abstracts away root selection.
*
* @param $data
* @return BSTNode
* @throws Exception
@KerryJones
KerryJones / tower-of-hanoi.php
Created December 4, 2015 10:48
Tower of Hanoi with Stacks
/**
* Childs game Tower of Hanoi with Stacks
* O(n^2)
*
*
* Disk layout
*
* 1|1 | |
* 2_|_2 | |
* 3__|__3 | |
@KerryJones
KerryJones / dfs-tree.php
Created December 8, 2015 04:44
PHP DFS on a Tree
function DFS( $node, $data ) {
if ( $node->data == $data )
return $node;
$new_node = null;
if ( !is_null( $node->left ) )
$new_node = DFS($node->left, $data);
if ( !$new_node && !is_null( $node->right ) )
@KerryJones
KerryJones / bit-manipulations.php
Created December 10, 2015 09:01
PHP Bit Manipulations
function getBit($binary_number, $byte_placement) {
return ( $binary_number & ( 1 << $byte_placement ) ) != 0;
}
function setBit( $binary_number, $byte_placement ) {
return $binary_number | ( 1 << $byte_placement );
}
function clearBit( $binary_number, $byte_placement ) {
$mask = ~( 1 << $byte_placement );