Skip to content

Instantly share code, notes, and snippets.

View KerryJones's full-sized avatar

Kerry Jones KerryJones

View GitHub Profile
@KerryJones
KerryJones / insertion-sort.py
Last active December 19, 2018 16:56
Python Insertion Sort
##
# Insertion Sort
#
# Runtime complexity: O(n^2)
# Space complexity: O(1)
##
def insertion_sort(arr, detail = False):
for i in range(1, len(arr)):
j = i
@KerryJones
KerryJones / selection-sort.py
Last active November 2, 2018 19:33
Python: Selection Sort
##
# Selection Sort
#
# Runtime Complexity: O(n^2)
# Space Complexity: O(1)
##
def selectionSort(arr, detail = False):
for i in range(len(arr)):
min = i
@KerryJones
KerryJones / sieve-of-eratosthenes.php
Created December 31, 2015 08:11
PHP: Sieve of Eratosthenes
<?php
function sieve_of_eratosthenes($max) {
$flags = array_fill(0, $max, true);
$flags[0] = $flags[1] = false;
foreach ( $flags as $index => &$is_prime ) {
if ( $is_prime ) {
echo $index . "<br>\n";
for( $i = $index*$index; $i < $max; $i +=$index )
@KerryJones
KerryJones / sieve-of-eratosthenes.py
Created December 31, 2015 07:53
Sieve of Eratosthenes
def sieve_of_eratosthenes(max):
flags = [True] * max
flags[0] = flags[1] = False
for (i, isPrime) in enumerate(flags):
if isPrime:
print(i)
for n in range(i*i, max, i):
flags[n] = False
@KerryJones
KerryJones / prime.py
Last active December 31, 2015 07:18
Python: Determine if a number is a prime
def prime(n):
if n == 2:
return True
elif n % 2 == 0:
return False
max = math.ceil(math.sqrt(n))
for i in range(3,max,2):
if n % i == 0:
@KerryJones
KerryJones / binary-search-tree.py
Created December 31, 2015 06:58
Python Data Structure: Binary Search Tree
class BST(object):
root = None
def search(self, data):
return self._binarySearch(data, self.root)
def _binarySearch(self, data, root):
if root is None:
return False
elif root.data == data:
@KerryJones
KerryJones / stack.py
Created December 31, 2015 05:35
Python Data Structure for Stack
class Stack(object):
top = None
def push(self, data):
node = Node(data)
node.next = self.top
self.top = node
def pop(self):
if self.top is None:
@KerryJones
KerryJones / linked-list.py
Created December 31, 2015 05:28
Python Linked List
class LinkedList(object):
head = None
def add(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
node = self.head
@KerryJones
KerryJones / quick-sort.php
Created December 15, 2015 08:58
PHP Quick Sort
<?php
function QuickSort(&$array, $start, $end) {
if ( $start < $end ) {
$pivotIndex = partition($array, $start, $end);
QuickSort($array, $start, $pivotIndex - 1);
QuickSort($array, $pivotIndex + 1, $end);
}
}
function partition(&$array, $start, $end) {
@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 );