>>> insertion_sort([-91, -123, -1]) [-123, -91, -1] >>> insertion_sort([]) [] """ for i in range(1, len(collection)): if verbose: print("Rotation " + str(i)) n = collection[i] # j from i - 1 to 0. for j in range(i - 1, -2, -1): if collection[j] <= n: break collection[j + 1] = collection[j] if verbose: print(collection) collection[j + 1] = n if verbose: print(collection) return collection if __name__ == "__main__": from common.invoker import from_input from_input(insertion_sort)
""" def counting_sort(collection, verbose=False): """Implementation of counting sort in Python. Args: collection (list): Input to sort. verbose (bool): Print every rotation if true. Returns: list: The same as the collection, with sort ascending applied. Example: >>> counting_sort([3, 1, 7, 0, 4, 8, 2]) [0, 1, 2, 3, 4, 7, 8] >>> counting_sort([-91, -123, -1]) [-123, -91, -1] >>> counting_sort([]) [] """ return collection if __name__ == "__main__": from common.invoker import from_input from_input(counting_sort)
left = 0 right = size - 1 for i in range(0, (size >> 1) + 1): if direction: if verbose: print("To right!") for j in range(left, size - 1): # 0 to size -1 if collection[j] > collection[j + 1]: collection[j], collection[j + 1] = collection[j + 1], collection[j] if verbose: print(collection) right -= 1 else: if verbose: print("To left!") for j in range(right, 0, -1): # size - 1 to 1 if collection[j - 1] > collection[j]: collection[j - 1], collection[j] = collection[j], collection[j - 1] if verbose: print(collection) left += 1 if right - left <= 1: break direction = not direction return collection if __name__ == "__main__": from common.invoker import from_input from_input(cocktail_shaker)
def radix_sort(collection, verbose=False): """Implementation of radix_sort in Python. Args: collection (list): Input to sort. verbose (bool): Print every rotation if true. Returns: list: The same as the collection, with sort ascending applied. Example: >>> radix_sort([3, 1, 7, 0, 4, 8, 2]) [0, 1, 2, 3, 4, 7, 8] >>> radix_sort([-91, -123, -1]) [-123, -91, -1] >>> radix_sort([]) [] """ return collection if __name__ == "__main__": from common.invoker import from_input from_input(radix_sort)
return "Success" else: return "Fail" @staticmethod def insert_test(): dict: Patricia = Patricia() dict.insert(Bitskey(1)) dict.insert(Bitskey(19)) dict.insert(Bitskey(5)) dict.insert(Bitskey(18)) dict.insert(Bitskey(3)) dict.insert(Bitskey(26)) dict.insert(Bitskey(9)) dict.dump() if __name__ == "__main__": from common.invoker import from_input trie = Patricia() insert_generic = lambda key: trie.insert if key.isdigit() else trie.insert_char search_generic = lambda key: trie.search if key.isdigit() else trie.search_char key_transform = lambda key: Bitskey(int(key)) if key.isdigit() else key insert = lambda key, verbose: insert_generic(key)(key_transform(key), verbose) search = lambda key, verbose: True if search_generic(key)(key_transform(key), verbose) == key_transform(key) else False from_input(insert, search)
def merge_sort(collection, verbose=False): """Implementation of merge_sort in Python. Args: collection (list): Input to sort. verbose (bool): Print every rotation if true. Returns: list: The same as the collection, with sort ascending applied. Example: >>> merge_sort([3, 1, 7, 0, 4, 8, 2]) [0, 1, 2, 3, 4, 7, 8] >>> merge_sort([-91, -123, -1]) [-123, -91, -1] >>> merge_sort([]) [] """ merge_sort_recursive(collection, 0, len(collection) - 1, verbose, 0) return collection if __name__ == "__main__": from common.invoker import from_input from_input(merge_sort)
>>> shell_sort([-91, -123, -1]) [-123, -91, -1] >>> shell_sort([]) [] """ size = len(collection) gap = round_odd(size >> 1) rotation = 1 while gap > 0: if verbose: print("Rotation " + str(rotation)) gap = round_odd(gap) # Number of subarrays: gap for i in range(0, gap + 1): subarray_insertion_sort(collection, i, size - 1, gap, verbose) gap = gap >> 1 rotation += 1 return collection if __name__ == "__main__": from common.invoker import from_input from_input(shell_sort)
winner_index = tree[-1] while winner_index is not None: if verbose: print("Run tournament.") print("Winner is " + str(collection[winner_index]) + ".") print("The tree looks like:") print_tournament_tree(collection, tree) print("") # Add winner to the output. output.append(collection[winner_index]) # The winner_index is not only an index of collection, # but also an index of the tree. # It is possible to locate index of the winner in the tree # because the winner comes from the leaf and the leafs form # a range from 0 to len(collection) - 1. # Unless the root is None, we can figure out where the winner came from. tree[winner_index] = None retrace_tournament_tree(collection, tree, winner_index) winner_index = tree[-1] return output if __name__ == "__main__": from common.invoker import from_input from_input(tournament_sort)
verbose (bool): Print every rotation if true. Returns: list: The same as the collection, with sort ascending applied. Example: >>> bubble_sort([3, 1, 7, 0, 4, 8, 2]) [0, 1, 2, 3, 4, 7, 8] >>> bubble_sort([-91, -123, -1]) [-123, -91, -1] >>> bubble_sort([]) [] """ for i in range(0, len(collection) - 1): if verbose: print("Rotation " + str(i + 1)) for j in range(0, len(collection) - 1): if (collection[j] > collection[j + 1]): collection[j], collection[j + 1] = collection[j + 1], collection[j] if verbose: print(collection) return collection if __name__ == "__main__": from common.invoker import from_input from_input(bubble_sort)
h = stack.pop() l = stack.pop() # No need for partition for subarray of size 1 or 0. if l >= h: continue if verbose: print(" " * len(stack) + "Partition " + str(collection[l:h + 1]), end="") p = partition(collection, l, h) if verbose: print(", pivot is " + str(collection[p])) # Elements on left if l < p - 1: stack.append(l) stack.append(p - 1) # Elements on right if p + 1 < h: stack.append(p + 1) stack.append(h) return collection if __name__ == "__main__": from common.invoker import from_input from_input(quick_sort)
def [blahblah](collection, verbose=False): """Implementation of [blahblah] in Python. Args: collection (list): Input to sort. verbose (bool): Print every rotation if true. Returns: list: The same as the collection, with sort ascending applied. Example: >>> [blahblah]([3, 1, 7, 0, 4, 8, 2]) [0, 1, 2, 3, 4, 7, 8] >>> [blahblah]([-91, -123, -1]) [-123, -91, -1] >>> [blahblah]([]) [] """ return collection if __name__ == "__main__": from common.invoker import from_input from_input([blahblah])
while i < len(runs): # Leave last single item to the next stage. if i + 1 >= len(runs): merged_runs.append(runs[-1]) break left = runs[i][0] middle = runs[i][1] right = runs[i + 1][1] if verbose: print(" Merge " + str(collection[left:middle+1]) + " and " + str(collection[middle+1:right+1])) merge(collection, left, middle, right, verbose, 2) merged_runs.append((left, right)) i += 2 runs = merged_runs return collection if __name__ == "__main__": from common.invoker import from_input from_input(natural_merge_sort)
verbose (bool): Print every rotation if true. Returns: list: The same as the collection, with sort ascending applied. Example: >>> exchange_sort([3, 1, 7, 0, 4, 8, 2]) [0, 1, 2, 3, 4, 7, 8] >>> exchange_sort([-91, -123, -1]) [-123, -91, -1] >>> exchange_sort([]) [] """ size = len(collection) for i in range(0, size - 1): for j in range(i, size): if collection[i] > collection[j]: collection[i], collection[j] = collection[j], collection[i] if verbose: print(collection) return collection if __name__ == "__main__": from common.invoker import from_input from_input(exchange_sort)
[0, 1, 2, 3, 4, 7, 8] >>> selection_sort([-91, -123, -1]) [-123, -91, -1] >>> selection_sort([]) [] """ for i in range(0, len(collection) - 1): if verbose: print("Rotation " + str(i + 1)) min_index = i # Find the index of the minimum item. for j in range(i, len(collection)): if collection[j] < collection[min_index]: min_index = j # Swap if found something smaller than it has. if min_index != i: collection[min_index], collection[i] = collection[i], collection[ min_index] if verbose: print(collection) return collection if __name__ == "__main__": from common.invoker import from_input from_input(selection_sort)