Skip to content

Exercise aimed to review sorting algorithms, unit testing, and function profiling using Python 3.12 new features.

License

Notifications You must be signed in to change notification settings

JCPedroza/unit-test-and-profile-sorting-algorithms-py-3-12

Repository files navigation

Implementing, Testing, & Timing Sorting Algorithms Using Python's 3.12 New Features

python license MIT style black lint: flake8 test: unittest

This project is a quick review of:

  • Python's 3.12 new features
  • Python's native unit testing module unittest
  • Python's type hint system
  • Measuring the running time of a function
  • Six basic sorting algorithms

This project also serves as the notes of said review. You'll find some theory in the comments, docstrings, and readme.

The six basic sorting algorithms reviewed:

  • Selection Sort
  • Bubble Sort
  • Insertion Sort
  • Merge Sort
  • Heap Sort
  • Quick Sort

Only one version of each algorithm is chosen and implemented.

Installation

Installation is required to run this program. Once you clone this repository, install the dependencies with:

python -m pip install -r requirements.txt

How to Run

The main functionality of the program is to measure and compare the running time of different sorting algorithms. To run using default values use:

python main.py

If you want to provide your own values, the program accepts two command line arguments: the size of the list to be sorted, and the number of times that the measurement will be repeated and accumulated:

python main.py 20 50

The above will sort arrays of size 20, 50 different times, and the results added together.

Static Type Check

Note that mypy might not yet support type aliases, but pyright already does.

python -m pyright .
python -m mypy .

Linting & Formatting

python -m black .
python -m flake8 .

Unit Testing

This project uses the unittest native Python module to write and run unit testing.

To run tests use either:

python sort_test.py
python -m unittest sort_test.py

Analysis of Used Implementations

algorithm time complexity (best avg worst) space complexity (total aux)
selection sort O(n^2) O(n^2) O(n^2) O(n) O(1)
bubble sort O(n) O(n^2) O(n^2) O(n) O(1)
insertion sort O(n) O(n^2) O(n^2) O(n) O(1)
merge sort O(n log n) O(n log n) O(n log n) O(n) O(n)
heap sort O(n log n) O(n log n) O(n log n) O(n) O(1)
quick sort O(n log n) O(n log n) O(n^2) O(n) O(n)

Contributing

Found a bug, typo, or mistake? Want to refactor, optimize, or improve something in this repository? Send a pull request! Pull requests are always welcome!

There's no need to create an issue. Just use a descriptive commit message and I'll format it adequately when accepting the pull request. Contributing here is as simple as commiting your changes and sending a pull request!

Resources

Python

What's New in Python 3.12

Algorithms & Data Structures

Selection Sort

Bubble Sort

Insertion Sort

Merge Sort

Heap Sort

Quick Sort


About

Exercise aimed to review sorting algorithms, unit testing, and function profiling using Python 3.12 new features.

Topics

Resources

License

Stars

Watchers

Forks

Languages