Skip to content

Instantly share code, notes, and snippets.

View KerryJones's full-sized avatar

Kerry Jones KerryJones

View GitHub Profile
@KerryJones
KerryJones / bfs.py
Last active July 28, 2020 05:32
Python Breadth-First-Search
class Node:
visited = None
data = None
adjacent = None
def __init__(self, value):
self.data = value
self.adjacent = []
self.visited = False
from typing import List
def quick_select(array: List, start: int, end: int, kth: int):
if start > end:
return
pivot = partition(array, start, end)
if pivot == kth - 1:
@KerryJones
KerryJones / merge-sort.py
Last active July 28, 2020 04:56
Python Merge Sort
def merge_sort(array: List) -> List:
if len(array) < 2:
return array
middle = floor(len(array)/2)
left = merge_sort(array[:middle])
right = merge_sort(array[middle:])
sorted_array = []
from typing import List
def quick_sort(array: List, start: int, end: int) -> None:
if start >= end:
return
pivot = partition(array, start, end)
quick_sort(array, start, pivot - 1)
quick_sort(array, pivot + 1, end)
@KerryJones
KerryJones / bfs-tree.php
Last active September 12, 2019 05:54
PHP BFS on a Tree
function BFS( $node, $data ) {
if ( $node->data == $data )
return $node;
$node->visited = true;
$queue = new Queue;
$queue->enqueue($node);
while ( !$queue->isEmpty() ) {
@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 / disjoint-set.py
Last active December 19, 2018 16:52
Python 3 - Disjoint Set
class DisjointSet:
sets = {}
rank = {}
def __init__(self, arr):
for v in arr:
self.sets[v] = v
self.rank[v] = 0
def merge(self, v1, v2):
@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 / data-structure-queue.php
Created December 4, 2015 06:46
PHP Data Structure: Queue
class Queue {
protected $first;
protected $last;
public function enqueue($data) {
$node = new Node($data);
if ( is_null( $this->first ) ) {
$this->first = $this->last = $node;
} else {
@KerryJones
KerryJones / MySQLUpsert.php
Created January 7, 2017 02:14
Laravel 5.3 MySQL Upsert Trait
<?php
namespace App\Traits;
use Illuminate\Support\Facades\DB;
trait MySQLUpsert
{
/**
* Single call to insert/update based on any duplicate key (primary, unique, etc.)