Skip to main content

A calculator to predict big-O of sorting functions

Project description

Big-O Caculator

A big-O calculator to estimate time complexity of sorting functions.

inspired by : https://github.com/ismaelJimenez/cpp.leastsq

Installation

pip install big-O-calculator

Usage

You can call which array to test

(n : [10, 100, 1_000, 10_000, 100_000])

Big-O calculator

Args:
    functionName [Callable]: a function to call.

    array [str]: "random", "sorted", "reversed", "partial", "Ksorted".

    limit [bool] = True: To break before it takes "forever" to sort an array. (ex. selectionSort)

Returns:
    complexity (str) : ex) O(n)

    time (float) : Time took to sort all 5 different arrays in second (maxLen=100,000)

Info:
    To see the result of function, return the array.

    K in Ksorted will use testSize.bit_length().
from bigO import bigO

tester = bigO.bigO()

tester.test(bubbleSort, "sorted")

Quick Sort Example

from bigO import bigO
from random import randint

def quickSort(array):  # in-place | not-stable
    """
    Best : O(nlogn) Time | O(logn) Space
    Average : O(nlogn) Time | O(logn) Space
    Worst : O(n^2) Time | O(logn) Space
    """
    if len(array) <= 1:
        return array
    smaller, equal, larger = [], [], []
    pivot = array[randint(0, len(array) - 1)]
    for x in array:
        if x < pivot:
            smaller.append(x)
        elif x == pivot:
            equal.append(x)
        else:
            larger.append(x)
    return quickSort(smaller) + equal + quickSort(larger)


tester = bigO.bigO()
complexity, time = tester.test(quickSort, "random")
complexity, time = tester.test(quickSort, "sorted")
complexity, time = tester.test(quickSort, "reversed")
complexity, time = tester.test(quickSort, "partial")
complexity, time = tester.test(quickSort, "Ksorted")

''' Result
Running quickSort(random array)...
Completed quickSort(random array): O(nlog(n))
Time took: 0.35816s
Running quickSort(sorted array)...
Completed quickSort(sorted array): O(nlog(n))
Time took: 0.37821s
Running quickSort(reversed array)...
Completed quickSort(reversed array): O(nlog(n))
Time took: 0.38500s
Running quickSort(partial array)...
Completed quickSort(partial array): O(nlog(n))
Time took: 0.35820s
Running quickSort(Ksorted array)...
Completed quickSort(ksorted array): O(nlog(n))
Time took: 0.38140s
'''

Selection Sort Example

from bigO import bigO

def selectionSort(array):  # in-place, unstable
    '''
    Best : O(n^2) Time | O(1) Space
    Average : O(n^2) Time | O(1) Space
    Worst : O(n^2) Time | O(1) Space
    '''
    currentIdx = 0
    while currentIdx < len(array) - 1:
        smallestIdx = currentIdx
        for i in range(currentIdx + 1, len(array)):
            if array[smallestIdx] > array[i]:
                smallestIdx = i
        array[currentIdx], array[smallestIdx] = array[smallestIdx], array[currentIdx]
        currentIdx += 1
    return array


tester = bigO.bigO()
complexity, time = tester.test(selectionSort, "random")
complexity, time = tester.test(selectionSort, "sorted")
complexity, time = tester.test(selectionSort, "reversed")
complexity, time = tester.test(selectionSort, "partial")
complexity, time = tester.test(selectionSort, "Ksorted")

''' Result
Running selectionSort(random array)...
Completed selectionSort(random array): O(n^2)
Time took: 4.04027s
Running selectionSort(reversed array)...
Completed selectionSort(reversed array): O(n^2)
Time took: 4.04918s
Running selectionSort(sorted array)...
Completed selectionSort(sorted array): O(n^2)
Time took: 3.97238s
Running selectionSort(partial array)...
Completed selectionSort(partial array): O(n^2)
Time took: 4.02878s
Running selectionSort(Ksorted array)...
Completed selectionSort(ksorted array): O(n^2)
Time took: 4.05617s
'''

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

big-O calculator-0.0.7.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

big_O_calculator-0.0.7.1-py3.7.egg (17.8 kB view details)

Uploaded Egg

big_O_calculator-0.0.7.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file big-O calculator-0.0.7.1.tar.gz.

File metadata

  • Download URL: big-O calculator-0.0.7.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for big-O calculator-0.0.7.1.tar.gz
Algorithm Hash digest
SHA256 a9a3234388def44ca6d6d724190ffb89b816d821da0d4e4b62421399318c8429
MD5 7c9bb56999e37acda3573668c15e5f65
BLAKE2b-256 f615567fbf2af152f37d43e295dae6447e367d62b248150916e8feced8bcd8cf

See more details on using hashes here.

File details

Details for the file big_O_calculator-0.0.7.1-py3.7.egg.

File metadata

  • Download URL: big_O_calculator-0.0.7.1-py3.7.egg
  • Upload date:
  • Size: 17.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for big_O_calculator-0.0.7.1-py3.7.egg
Algorithm Hash digest
SHA256 0fc12b30b52350cb97ecedb2df8f18f9c955d0b42e7bc201e480d06b4726e5df
MD5 0814d38260c1db6be0a4288e1466ee2e
BLAKE2b-256 7c04266d0ab323442ba6bb78aab6e7689d85b625737c1ba41e681e86d120115e

See more details on using hashes here.

File details

Details for the file big_O_calculator-0.0.7.1-py3-none-any.whl.

File metadata

  • Download URL: big_O_calculator-0.0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for big_O_calculator-0.0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 56900049ba78966b6a851b82b524f1f39ba34c1c6c94dfe9d726ef7b273e3e59
MD5 bbfd30880958c55f82eef04efa2660bb
BLAKE2b-256 a79b8dcb915f4f2ca9f52f630ee06c8427cf1641302283e0466de32cdf3e1765

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page