return count def to_string_list(items): return [str(item) for item in items] if __name__ == "__main__": print("Integer keys") time_us( { "Lin. search": lambda args: linear_search(*args), "Hash search": lambda args: hash_search(100, lambda x: x % 100, *args), }, ns=powers_of(10, 0, 5), generator=lambda n: (shuffled_ints(n), random_ints(1000)), repeats=10) print("String keys") time_us( { "Lin. search": lambda args: linear_search(*args), "Hash search": lambda args: hash_search(100, lambda x: len(x) % 100, *args), }, ns=powers_of(10, 0, 5), generator=lambda n: (random_strings(n, 3), random_strings(1000, 3)), repeats=10)
elif len(lst) % 2 == 0: return foldr(merge, [], split(lst)) else: popped = lst[0] lst.pop(0) return selection_sort([popped] + foldr(merge, [], split(lst))) def quick_sort(lst): """Quick sort chooses a pivot element, places the elements greater than and less than the pivot into places relative to the pivot, and are then recursively sorted.""" lst1 = [x for x in lst[1:] if x <= lst[0]] lst2 = [y for y in lst if y > lst[0]] if len(lst) == 0: return lst elif len(lst) == 1: return lst else: return quick_sort(lst1) + [lst[0]] + quick_sort(lst2) if __name__ == "__main__": time_us({ "Bubble sort": bubble_sort, "Std sort": sorted }, ns=powers_of(10, 0, 4), generator=random_ints, repeats=10)
def radixsort(a, get_keys=(lambda item: item[0], lambda item: item[1])): for get_key in get_keys: maxLen = -1 for number in a: # Find longest number, in digits numLen = len(str(number)) if numLen > maxLen: maxLen = numLen buckets = [[] for i in range(0, 10)] # Buckets for each digit for digit in range(0, maxLen): for number in a: buckets[int(get_key(number) / 10 ** digit % 10)].append(number) del a[:] for bucket in buckets: a.extend(bucket) del bucket[:] return a if __name__ == "__main__": time_us({ "Std sort": sorted, "Cnt. sort": counting_sort #,"Bucket sort": bucket_sort }, ns=powers_of(10, 0, 5), generator=lambda n: random_ints(n, -1000, 1000), repeats=100) # # time_us({ # "Std sort": sorted, # "Radix sort": radix_sort # }, ns=powers_of(10, 0, 5), generator=lambda n: random_int_pairs(n, -1000, 1000), repeats=100)
while j < len(righthalf): items[k] = righthalf[j] j = j + 1 k = k + 1 print("Merging ", items) result = items # Your code here return result def quick_sort(items, cmp=lambda x, y: x < y): """ Quick sort :param items: list of items to sort_func :param cmp: compare function, cmp(x,y) returns True if x < y, False otherwise :return: sorted items """ result = items # Your code here return result if __name__ == "__main__": time_us({ "Bubble sort": bubble_sort, "Std sort": sorted }, ns=powers_of(10, 0, 4), generator=random_ints, repeats=10)
from mpiaa.search.bstree import BSTree from mpiaa.timer import time_us from mpiaa.util import shuffled_ints, random_ints, powers_of def linear_search(items, items_to_find): count = 0 for item_to_find in items_to_find: if item_to_find in items: count += 1 return count def binary_search(items, items_to_find): tree = BSTree() for item in items: tree.insert(item) count = 0 for item_to_find in items_to_find: if tree.find(item_to_find) is not None: count += 1 return count if __name__ == "__main__": time_us({ "Lin. search": lambda args: linear_search(*args), "Bin. search": lambda args: binary_search(*args), }, ns=powers_of(10, 0, 5), generator=lambda n: (shuffled_ints(n), random_ints(1000)), repeats=10)