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
## | |
# 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
<?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 ) |
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 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 |
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 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: |
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 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: |
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 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: |
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 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 |
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 | |
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) { |
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 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 ); |