def test_intro(BigO): # Results may vary, O(n) possible complexity = BigO.test(algorithm.introSort, "random") complexity = BigO.test(algorithm.introSort, "sorted") complexity = BigO.test(algorithm.introSort, "reversed") complexity = BigO.test(algorithm.introSort, "partial") complexity = BigO.test(algorithm.introSort, "Ksorted")
def test_quickcomp(BigO): # Hoare + Tail recur should be faster than random pivot choosing recursive one result = BigO.compare(algorithm.quickSort, algorithm.quickSortHoare, "random", 50000) result = BigO.compare(algorithm.quickSortHoare, algorithm.quickSortHeap, "random", 50000) result = BigO.compare(algorithm.quickSort, algorithm.quickSortHoare, "reversed", 50000) result = BigO.compare(algorithm.quickSortHoare, algorithm.quickSortHeap, "reversed", 50000) result = BigO.compare(algorithm.quickSort, algorithm.quickSortHoare, "sorted", 50000) result = BigO.compare(algorithm.quickSortHoare, algorithm.quickSortHeap, "sorted", 50000) result = BigO.compare(algorithm.quickSort, algorithm.quickSortHoare, "partial", 50000) result = BigO.compare(algorithm.quickSortHoare, algorithm.quickSortHeap, "partial", 50000) result = BigO.compare(algorithm.quickSort, algorithm.quickSortHoare, "Ksorted", 50000) result = BigO.compare(algorithm.quickSortHoare, algorithm.quickSortHeap, "Ksorted", 50000) print(result)
def test_str_array(BigO): time, result = BigO.runtime(algorithm.bubbleSort, "string", 10) print(result) time, result = BigO.runtime(algorithm.introSort, "string", 10) print(result) time, result = BigO.runtime(algorithm.quickSort, "string", 10) print(result)
def test_all(BigO): result = BigO.compare(algorithm.quickSortHoare, algorithm.quickSortHeap, "all", 50000) result = BigO.compare(algorithm.insertSort, algorithm.bubbleSort, "all", 5000) print(result) result = BigO.compare(algorithm.quickSortHoare, algorithm.insertSort, "all", 5000)
def test_count(BigO): # Results may vary complexity = BigO.test(algorithm.countSort, "random") assert complexity == "O(n)" complexity = BigO.test(algorithm.countSort, "sorted") assert complexity == "O(n)" complexity = BigO.test(algorithm.countSort, "reversed") assert complexity == "O(n)" complexity = BigO.test(algorithm.countSort, "partial") assert complexity == "O(n)" complexity = BigO.test(algorithm.countSort, "Ksorted") assert complexity == "O(n)"
def test_quickSort(BigO): complexity = BigO.test(algorithm.quickSortHoare, "random") complexity = BigO.test(algorithm.quickSortHoare, "sorted") complexity = BigO.test(algorithm.quickSortHoare, "reversed") complexity = BigO.test(algorithm.quickSortHoare, "partial") complexity = BigO.test(algorithm.quickSortHoare, "Ksorted") complexity = BigO.test(algorithm.quickSortHoare, "hole") complexity = BigO.test(algorithm.quickSortHoare, "equal") complexity = BigO.test(algorithm.quickSortHoare, "almost_equal") complexity = BigO.test(algorithm.quickSortHoare, "string")
def test_Ksorted(BigO): arr = BigO.genKsortedArray(9, 1) assert isKSortedArray(arr, 1) == True arr = BigO.genKsortedArray(100, 9) assert isKSortedArray(arr, 9) == True arr = BigO.genKsortedArray(9, 9) assert isKSortedArray(arr, 9) == True arr = BigO.genKsortedArray(10, 5) assert isKSortedArray(arr, 5) == True arr = BigO.genKsortedArray(1000, 100) assert isKSortedArray(arr, 100) == True
def test_bubble(BigO): complexity = BigO.test(algorithm.bubbleSort, "random") complexity = BigO.test(algorithm.bubbleSort, "sorted") complexity = BigO.test(algorithm.bubbleSort, "reversed") complexity = BigO.test(algorithm.bubbleSort, "partial") complexity = BigO.test(algorithm.bubbleSort, "Ksorted") complexity = BigO.test(algorithm.bubbleSort, "almost_equal") complexity = BigO.test(algorithm.bubbleSort, "equal") complexity = BigO.test(algorithm.bubbleSort, "hole")
def wrapper(array, *args): lib = BigO() result = func(array, *args) _sorted, index = lib.isAscendingSorted(result) if index == 1: msg = f"{result[index - 1]}, {result[index]}..." elif index == len(result) - 1: msg = f"...{result[index - 1]}, {result[index]}" elif isinstance(index, int): msg = f"...{result[index - 1]}, {result[index]}, {result[index + 1]}..." if not _sorted: # Just see the result if it doesn't sort correctly print( f"{func.__name__} doesn't sort correctly.\nAt {index} index: [{msg}]" ) else: print(f"{func.__name__} sorts correctly.")
def test_insertion(BigO): complexity = BigO.test(algorithm.insertSort, "random") complexity = BigO.test(algorithm.insertSort, "sorted") complexity = BigO.test(algorithm.insertSort, "reversed") complexity = BigO.test(algorithm.insertSort, "partial") complexity = BigO.test(algorithm.insertSort, "Ksorted") complexity = BigO.test(algorithm.insertSort, "string")
def test_quickSort(BigO): complexity = BigO.test(algorithm.quickSort, "random") complexity = BigO.test(algorithm.quickSort, "sorted") complexity = BigO.test(algorithm.quickSort, "reversed") complexity = BigO.test(algorithm.quickSort, "partial") complexity = BigO.test(algorithm.quickSort, "Ksorted") complexity = BigO.test(algorithm.quickSort, "string")
def test_timsort(BigO): # Results may vary complexity = BigO.test(algorithm.timSort, "random") complexity = BigO.test(algorithm.timSort, "sorted") complexity = BigO.test(algorithm.timSort, "reversed") complexity = BigO.test(algorithm.timSort, "partial") complexity = BigO.test(algorithm.timSort, "Ksorted") complexity = BigO.test(algorithm.timSort, "hole")
def test_run(BigO): result = BigO.compare(algorithm.bubbleSort, algorithm.insertSort, "reversed", 5000) result = BigO.compare(algorithm.insertSort, algorithm.insertSortOptimized, "reversed", 5000) result = BigO.compare(algorithm.quickSort, algorithm.quickSortHoare, "reversed", 50000) result = BigO.compare(algorithm.timSort, algorithm.introSort, "reversed", 50000) result = BigO.compare(sorted, algorithm.introSort, "reversed", 50000) result = BigO.compare(algorithm.heapSort, algorithm.heapSort2, "all", 50000) result = BigO.compare(algorithm.introSort, algorithm.quickSortHeap, "all", 50000)
def test_none(BigO): def countSort(arr): # stable # Time Complexity : O(n) | Space Complexity : O(n) minValue = min(arr) maxValue = max(arr) - minValue buckets = [0 for _ in range(maxValue + 1)] for i in arr: buckets[i - minValue] += 1 index = 0 for i in range(len(buckets)): while buckets[i] > 0: arr[index] = i + minValue index += 1 buckets[i] -= 1 _, _ = BigO.test(countSort, "random") # will return a warning
from bigO import BigO from random import randint from sorting_algorithms import * lib = BigO() complexity = lib.test(bucketsort, "random") # complexity = lib.test(countsort_neg, "sorted") # complexity = lib.test(countsort_neg, "reversed") # complexity = lib.test(countsort_neg, "partial") # complexity = lib.test(countsort_neg, "Ksorted") # lib.test_all(countsort)
from bigO import BigO from bigO import utils @utils.isSorted def bubbleSort(array): # in-place | stable isSorted = False counter = 1 # not correct while not isSorted: isSorted = True for i in range(len(array) - 1 - counter): if array[i] > array[i + 1]: array[i], array[i + 1] = array[i + 1], array[i] isSorted = False counter += 1 return array if __name__ == "__main__": bubbleSort(BigO.genRandomArray(100))
def big_o(fun, dta): print(BigO.test(fun, dta))
def test_custom(BigO): BigO.compare(algorithm.doubleSelectionSort, algorithm.selectionSort, "all", 5000)
def test_run(BigO): BigO.runtime(sorted, "random", 5000) BigO.runtime(algorithm.bubbleSort, "random", 5000) BigO.runtime(algorithm.countSort, "random", 5000) BigO.runtime(algorithm.binaryInsertSort, "random", 5000) BigO.runtime(algorithm.gnomeSort, "random", 5000) BigO.runtime(algorithm.heapSort, "random", 5000) BigO.runtime(algorithm.insertSort, "random", 5000) BigO.runtime(algorithm.insertSortOptimized, "random", 5000) BigO.runtime(algorithm.introSort, "random", 5000) BigO.runtime(algorithm.mergeSort, "random", 5000) BigO.runtime(algorithm.timSort, "random", 5000) BigO.runtime(algorithm.selectionSort, "random", 5000)
def test_mini(BigO): BigO.compare(algorithm.insertSortOptimized, algorithm.insertSort, "random", 16) BigO.compare(algorithm.bubbleSort, algorithm.insertSort, "random", 16) BigO.compare(algorithm.insertSort, algorithm.selectionSort, "random", 16) BigO.compare(algorithm.bubbleSort, algorithm.selectionSort, "random", 16) BigO.compare(algorithm.bubbleSort, algorithm.insertSort, "reversed", 16) BigO.compare(algorithm.insertSort, algorithm.selectionSort, "reversed", 16) BigO.compare(algorithm.bubbleSort, algorithm.selectionSort, "reversed", 16)
def test_go(BigO): BigO.compare(algorithm.goSort, algorithm.introSort, "random", 100000) BigO.compare(algorithm.goSort, algorithm.quickSortHoare, "random", 10000) BigO.compare(algorithm.goSort, algorithm.heapSort, "random", 10000) BigO.compare(algorithm.goSort, algorithm.timSort, "random", 10000) BigO.compare(algorithm.goSort, algorithm.quickSortHoare, "Ksorted", 10000) BigO.compare(algorithm.goSort, algorithm.quickSortHoare, "Ksorted", 10000)
def test_custom(BigO): arr = ["dbc", "bbc", "ccd", "ef", "az"] time, result = BigO.runtime(brokenBubble, arr) print(time) print(result)
def test_BigO(BigO): print(BigO.genRandomArray(20)) print(BigO.genRandomString(5, 20)) print(BigO.genSortedArray(20)) print(BigO.genReversedArray(20)) print(BigO.genPartialArray(20)) print(BigO.genKsortedArray(20, 6)) for i in range(21): arr = BigO.genKsortedArray(20, i) assert isKSortedArray(arr, i) == True print(BigO.genAlmostEqualArray(9)) print(BigO.genAlmostEqualArray(20)) print(BigO.genEqualArray(20)) print(BigO.genHoleArray(20)) print(BigO.genRandomBigArray(20))
def test_heap(BigO): BigO.runtime(algorithm.heapSort2, "random", 500)
def BigO(): from bigO import BigO lib = BigO() return lib
def test_Ksorted(BigO): # Results may vary, O(n) possible complexity = BigO.test(algorithm.introSort, "Ksorted") complexity = BigO.test(algorithm.quickSort, "Ksorted") complexity = BigO.test(algorithm.timSort, "Ksorted") complexity = BigO.test(algorithm.countSort, "Ksorted")
def test_big(BigO): BigO.runtime(algorithm.bubbleSort, "random", 5000) BigO.runtime(algorithm.bubbleSort, "big", 5000)
def test_brokenBubble(BigO): _ = BigO.test(brokenBubble, "random")
for x in inner_list: if x not in result: result.append(x) return result def f2(list_of_list): flat_list = [] for inner_list in list_of_list: flat_list.extend(inner_list) return [x for i, x in enumerate(flat_list) if flat_list.index(x) == i] def f3(list_of_list): result = [] seen = set() for inner_list in list_of_list: for x in inner_list: if x not in seen: result.append(x) seen.add(x) return result lib = BigO() # complexity = lib.test(f1, "random") complexity = lib.test(f1, "sorted") complexity = lib.test(f1, "reversed") complexity = lib.test(f1, "partial") complexity = lib.test(f1, "Ksorted")
def test_epoch(BigO): BigO.runtime(algorithm.quickSort, "random", 5000, 3, True) BigO.runtime(algorithm.quickSortHoare, "random", 5000, 3, True)