This file contains 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
class Node: | |
visited = None | |
data = None | |
adjacent = None | |
def __init__(self, value): | |
self.data = value | |
self.adjacent = [] | |
self.visited = False |
This file contains 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
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: |
This file contains 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
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 = [] |
This file contains 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
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) |
This file contains 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
function BFS( $node, $data ) { | |
if ( $node->data == $data ) | |
return $node; | |
$node->visited = true; | |
$queue = new Queue; | |
$queue->enqueue($node); | |
while ( !$queue->isEmpty() ) { |
This file contains 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
## | |
# 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 |
This file contains 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
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): |
This file contains 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
## | |
# Selection Sort | |
# | |
# Runtime Complexity: O(n^2) | |
# Space Complexity: O(1) | |
## | |
def selectionSort(arr, detail = False): | |
for i in range(len(arr)): | |
min = i |
This file contains 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
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 { |
This file contains 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
<?php | |
namespace App\Traits; | |
use Illuminate\Support\Facades\DB; | |
trait MySQLUpsert | |
{ | |
/** | |
* Single call to insert/update based on any duplicate key (primary, unique, etc.) |
NewerOlder