{"id":21906231,"url":"https://github.com/alfex4936/python-bigo-calculator","last_synced_at":"2025-07-21T20:31:33.303Z","repository":{"id":151852396,"uuid":"291040460","full_name":"Alfex4936/python-bigO-calculator","owner":"Alfex4936","description":"Python sorting functions time complexity calculator","archived":false,"fork":false,"pushed_at":"2023-01-07T06:51:32.000Z","size":275,"stargazers_count":27,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T23:56:33.455Z","etag":null,"topics":["algorithm","calculator","complexity","python","sorting-functions"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/big-O-calculator/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Alfex4936.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-08-28T12:25:33.000Z","updated_at":"2024-11-26T15:40:46.000Z","dependencies_parsed_at":"2023-05-12T11:45:26.530Z","dependency_job_id":null,"html_url":"https://github.com/Alfex4936/python-bigO-calculator","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/Alfex4936/python-bigO-calculator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alfex4936%2Fpython-bigO-calculator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alfex4936%2Fpython-bigO-calculator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alfex4936%2Fpython-bigO-calculator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alfex4936%2Fpython-bigO-calculator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Alfex4936","download_url":"https://codeload.github.com/Alfex4936/python-bigO-calculator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alfex4936%2Fpython-bigO-calculator/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266375198,"owners_count":23919539,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-21T11:47:31.412Z","response_time":64,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","calculator","complexity","python","sorting-functions"],"created_at":"2024-11-28T16:43:05.024Z","updated_at":"2025-07-21T20:31:33.280Z","avatar_url":"https://github.com/Alfex4936.png","language":"Python","readme":"## Table of Contents\n\n\u003ctable\u003e\n    \u003ctr\u003e\u003ctd width=40% valign=top\u003e\n        \n* [Methods](https://github.com/Alfex4936/python-bigO-calculator#methods-parameters)\n* [Usage](https://github.com/Alfex4936/python-bigO-calculator#usage)\n* [QuickSort example](https://github.com/Alfex4936/python-bigO-calculator#quick-sort-example)\n* [test_all() exmaple](https://github.com/Alfex4936/python-bigO-calculator#test_allmysort-example)\n* [runtime() exmaple](https://github.com/Alfex4936/python-bigO-calculator#runtimemysort-example)\n* [compare() exmaple](https://github.com/Alfex4936/python-bigO-calculator#comparemysort-thissort-example)\n* [@isSorted decorator](https://github.com/Alfex4936/python-bigO-calculator#issorted)\n* [Array generators](https://github.com/Alfex4936/python-bigO-calculator#array-generators)\n* [Test arrays sample](https://github.com/Alfex4936/python-bigO-calculator#test-arrays-sample-size--20)\n\u003c/td\u003e\u003c/tr\u003e\n\u003c/table\u003e\n\n# Big-O Caculator\n\nA big-O calculator to estimate time complexity of sorting functions.\n\ninspired by : https://github.com/ismaelJimenez/cpp.leastsq\n\n[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FAlfex4936%2Fpython-bigO-calculator\u0026count_bg=%233D6BC8\u0026title_bg=%23555555\u0026icon=\u0026icon_color=%23E7E7E7\u0026title=hits\u0026edge_flat=false)](https://hits.seeyoufarm.com) [![Downloads](https://pepy.tech/badge/big-o-calculator)](https://pepy.tech/project/big-o-calculator)\n\n## Installation\n\n```bash\npip install big-O-calculator\n```\n\n## What it does\n\nYou can test time complexity, calculate runtime, compare two sorting algorithms\n\nResults may vary.\n\n(n : [10, 100, 1_000, 10_000, 100_000])\n\n```py\nBig-O calculator\n\nMethods:\n    def test(function, array=\"random\", limit=True, prtResult=True):\n        It will run only specified array test, returns Tuple[str, estimatedTime]\n\n    def test_all(function):\n        It will run all test cases, prints (best, average, worst cases), returns dict\n\n    def runtime(function, array=\"random\", size, epoch=1):\n        It will simply returns execution time to sort length of size of test array, returns Tuple[float, List[Any]]\n\n    def compare(function1, function2, array, size, epoch=3):\n        It will compare two functions on {array} case, returns dict\n```\n\n## Methods parameters\n```py\ndef test(**args):\n    function [Callable]: a function to call.\n    array [str]: \"random\", \"big\", \"sorted\", \"reversed\", \"partial\", \"Ksorted\", \"string\", \"almost_equal\", \"equal\", \"hole\".\n    limit [bool] = True: To break before it takes \"forever\" to sort an array. (ex. selectionSort)\n    prtResult [bool] = True: Whether to print result by itself\n\ndef test_all(**args):\n    function [Callable]: a function to call.\n\ndef runtime(**args):\n    function [Callable]: a function to call.\n    array: \"random\", \"big\", \"sorted\", \"partial\", \"reversed\", \"Ksorted\" ,\n        \"hole\", \"equal\", \"almost_equal\" or your custom array.\n    size [int]: How big test array should be.\n    epoch [int]: How many tests to run and calculte average.\n    prtResult (bool): Whether to print the result by itself. (default = True)\n\ndef compare(**args):\n    function1 [Callable]: a function to compare.\n    function2 [Callable]: a function to compare.\n    array [str]|[List]: \"random\", \"big\", \"sorted\", \"partial\", \"reversed\", \"Ksorted\", \n        \"hole\", \"equal\", \"almost_equal\", \"all\" or your custom array.\n    size [int]: How big test array should be.\n```\nInfo:\n    To see the result of function, return the array.\n\n    These methods will also check if the function sorts correctly.\n\n    \"K\" in Ksorted uses testSize.bit_length().\n\n## Usage\n\n```py\nfrom bigO import BigO\nfrom bigO import algorithm\n\nlib = BigO()\n\nlib.test(bubbleSort, \"random\")\nlib.test_all(bubbleSort)\nlib.runtime(bubbleSort, \"random\", 5000)\nlib.runtime(algorithm.insertSort, \"reversed\", 32)\nlib.compare(algorithm.insertSort, algorithm.bubbleSort, \"all\", 5000)\n```\n\n## Quick Sort Example\n\n```py\nfrom bigO import BigO\nfrom random import randint\n\ndef quickSort(array):  # in-place | not-stable\n    \"\"\"\n    Best : O(nlogn) Time | O(logn) Space\n    Average : O(nlogn) Time | O(logn) Space\n    Worst : O(n^2) Time | O(logn) Space\n    \"\"\"\n    if len(array) \u003c= 1:\n        return array\n    smaller, equal, larger = [], [], []\n    pivot = array[randint(0, len(array) - 1)]\n    for x in array:\n        if x \u003c pivot:\n            smaller.append(x)\n        elif x == pivot:\n            equal.append(x)\n        else:\n            larger.append(x)\n    return quickSort(smaller) + equal + quickSort(larger)\n\n\nlib = BigO()\ncomplexity = lib.test(quickSort, \"random\")\ncomplexity = lib.test(quickSort, \"sorted\")\ncomplexity = lib.test(quickSort, \"reversed\")\ncomplexity = lib.test(quickSort, \"partial\")\ncomplexity = lib.test(quickSort, \"Ksorted\")\n\n''' Result\nRunning quickSort(random array)...\nCompleted quickSort(random array): O(nlog(n))\n\nRunning quickSort(sorted array)...\nCompleted quickSort(sorted array): O(nlog(n))\n\nRunning quickSort(reversed array)...\nCompleted quickSort(reversed array): O(nlog(n))\n\nRunning quickSort(partial array)...\nCompleted quickSort(partial array): O(nlog(n))\n\nRunning quickSort(Ksorted array)...\nCompleted quickSort(ksorted array): O(nlog(n))\n'''\n```\n\n## Selection Sort Example\n```py\nfrom bigO import BigO\n\ndef selectionSort(array):  # in-place, unstable\n    '''\n    Best : O(n^2) Time | O(1) Space\n    Average : O(n^2) Time | O(1) Space\n    Worst : O(n^2) Time | O(1) Space\n    '''\n    currentIdx = 0\n    while currentIdx \u003c len(array) - 1:\n        smallestIdx = currentIdx\n        for i in range(currentIdx + 1, len(array)):\n            if array[smallestIdx] \u003e array[i]:\n                smallestIdx = i\n        array[currentIdx], array[smallestIdx] = array[smallestIdx], array[currentIdx]\n        currentIdx += 1\n    return array\n\n\nlib = BigO()\ncomplexity = lib.test(selectionSort, \"random\")\ncomplexity = lib.test(selectionSort, \"sorted\")\ncomplexity = lib.test(selectionSort, \"reversed\")\ncomplexity = lib.test(selectionSort, \"partial\")\ncomplexity = lib.test(selectionSort, \"Ksorted\")\n\n''' Result\nRunning selectionSort(random array)...\nCompleted selectionSort(random array): O(n^2)\nRunning selectionSort(sorted array)...\nCompleted selectionSort(sorted array): O(n^2)\nRunning selectionSort(reversed array)...\nCompleted selectionSort(reversed array): O(n^2)\nRunning selectionSort(partial array)...\nCompleted selectionSort(partial array): O(n^2)\nRunning selectionSort(Ksorted array)...\nCompleted selectionSort(ksorted array): O(n^2)\n'''\n```\n\n## test_all(mySort) Example\n\nWe can test all \"random\", \"sorted\", \"reversed\", \"partial\", \"Ksorted\", \"almost_equal\" at once,\nand it shows, best, average and worst time complexity\n\n```py\nfrom bigO import BigO\n\nlib = BigO()\n\nlib.test_all(bubbleSort)\nlib.test_all(insertSort)\n\nresult = lib.test_all(selectionSort)\n\nprint(result)  # Dictionary\n\n\n''' Result\nRunning bubbleSort(tests)\nBest : O(n) Time\nAverage : O(n^2) Time\nWorst : O(n^2) Time\nRunning insertSort(tests)\nBest : O(n) Time\nAverage : O(n^2) Time\nWorst : O(n^2) Time\nRunning selectionSort(tests)\nBest : O(n^2) Time\nAverage : O(n^2) Time\nWorst : O(n^2) Time\n\n{'random': 'O(n^2)', 'sorted': 'O(n^2)', 'reversed': 'O(n^2)', 'partial': 'O(n^2)', 'Ksorted': 'O(n^2)'}\n'''\n```\n\n## runtime(mySort) Example\narray: \"random\", \"big\", \"sorted\", \"partial\", \"reversed\", \"Ksorted\",\n        \"hole\", \"equal\", \"almost_equal\" or your custom array.\n\n```py\nfrom bigO import BigO\nfrom bigO import algorithm\n\nlib = BigO()\n\ntimeTook, result = lib.runtime(algorithm.bubbleSort, \"random\", 5000)\n\ncustom = [\"abc\", \"bbc\", \"ccd\", \"ef\", \"az\"]\n\ntimeTook, result = lib.runtime(algorithm.bubbleSort, custom)\n\n''' Result\nRunning bubbleSort(len 5000 random array)\nTook 2.61346s to sort bubbleSort(random)\n\nRunning bubbleSort(len 5 custom array)\nTook 0.00001s to sort bubbleSort(custom)\n'''\n```\n\n## compare(mySort, thisSort) Example\narray: \"random\", \"big\", \"sorted\", \"partial\", \"reversed\", \"Ksorted\",\n        \"hole\", \"equal\", \"almost_equal\", \"all\" or your custom array.\n\n```py\nlib = BigO()\n\nresult = lib.compare(algorithm.bubbleSort, algorithm.insertSort, \"reversed\", 5000)\nresult = lib.compare(algorithm.insertSort, algorithm.insertSortOptimized, \"reversed\", 5000)\nresult = lib.compare(algorithm.quickSort, algorithm.quickSortHoare, \"reversed\", 50000)\nresult = lib.compare(algorithm.timSort, algorithm.introSort, \"reversed\", 50000)\nresult = lib.compare(sorted, algorithm.introSort, \"reversed\", 50000)\n\nresult = lib.compare(algorithm.bubbleSort, algorithm.insertSort, \"all\", 5000)\n\nprint(result)\n\n'''Result\nbubbleSort is 3.6% faster than insertSort on reversed case\nTime Difference: 0.04513s\ninsertSortOptimized is 5959.3% faster than insertSort on reversed case\nTime Difference: 1.25974s\nquickSortHoare is 153.6% faster than quickSort on reversed case\nTime Difference: 0.09869s\nintroSort is 206.6% faster than timSort on reversed case\nTime Difference: 0.12597s\nsorted is 12436.9% faster than introSort on reversed case\nTime Difference: 0.06862s\n\nRunning bubbleSort(tests) vs insertSort(tests)\ninsertSort is 32.6% faster than bubbleSort on 6 of 8 cases\nTime Difference: 0.11975s\n\n{'bubbleSort': 0.4875642249999998, 'insertSort': 0.3678110916666666}\n'''\n```\n\n## @isSorted\nIf it sorts correctly, it shows: \"mySort sorts correctly.\"\n\nOtherwise, it shows like,\n\"mySort doesn't sort correctly.\"\n\"At N index: [...100, -72, 121...]\n\n```py\nfrom bigO import BigO\nfrom bigO import utils\n\n@utils.is_sorted\ndef bubbleSort(array):  # in-place | stable\n    isSorted = False\n    counter = 1  # not correct\n    while not isSorted:\n        isSorted = True\n        for i in range(len(array) - 1 - counter):\n            if array[i] \u003e array[i + 1]:\n                array[i], array[i + 1] = array[i + 1], array[i]\n                isSorted = False\n\n        counter += 1\n\n    return array\n\n\nif __name__ == \"__main__\":\n    bubbleSort(BigO.gen_random_ints(100))\n\n''' Result\nbubbleSort doesn't sort correctly.\nAt 99 index: [...99, -76]\n'''\n```\n\n## Array generators\n```py\nfrom bigO import BigO\n\nlib = BigO()\n\narr = lib.gen_random_ints(100)\narr = lib.gen_random_big_ints(100)\narr = lib.gen_random_strings(100)\narr = lib.gen_sorted_ints(100)\narr = lib.gen_reversed_ints(100)\narr = lib.gen_partial_ints(100)\narr = lib.gen_ksorted_ints(100)\narr = lib.gen_equal_ints(100)\narr = lib.gen_almost_equal_ints(100)\narr = lib.gen_hole_ints(100)\n\n```\n\n## Test arrays sample (size = 20)\nResults vary.\n\n```py\nrandom = [15, 15, -11, -16, -19, -16, -14, 14, 19, 2, 18, -10, 5, -17, -4, -2, 9, 12, 8, 12]\nrandomBig = [-996061023766482, 347955820115093, ...]\nstring = ['rwe55pi8hkwpjv5rhhoo', '5ecvybo5xi8p25wanh3t', ...]\nsorted = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\nreversed = [19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\npartial = [-18, 14, 7, -11, 17, 5, 6, 7, 8, 9, 14, 9, -13, 0, 14, -17, -18, -9, -16, 14]\nKsorted = [-4, -5, -6, -7, -8, -9, -10, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 8]\nalmost_equal = [19, 19, 19, 20, 20, 19, 20, 20, 21, 19, 20, 21, 21, 19, 19, 21, 20, 19, 21, 19]\nequal = [16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16]\nhole = [-7, -7, -7, -7, -7, -7, -9999, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7]\n```\n\n## Built-in algorithms list\nVisit [here](https://github.com/Alfex4936/python-bigO-calculator/blob/master/bigO/algorithm.py) to see codes\n\nBinaryInsertSort, BubbleSort, CountSort, gnomeSort, heapSort, \nInsertSort, InsertSortOptimized, IntroSort,\nmergeSort, quickSort(random pivot), quickSortHoare(Hoare+Tail recur+InsertionSort), timSort(simplified)\n\n\n## Benchmarks\nVisit [here](https://github.com/Alfex4936/python-bigO-calculator/tree/master/benchmarks) for more results\n\n\u003cdiv align=\"center\"\u003e\n\u003cp\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/Alfex4936/python-bigO-calculator/master/benchmarks/random.webp\"\u003e\n\u003c/p\u003e\n\u003c/div\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falfex4936%2Fpython-bigo-calculator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falfex4936%2Fpython-bigo-calculator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falfex4936%2Fpython-bigo-calculator/lists"}