Skip to content

Compare the execution times of sorting algorithm implementations in Python.

Notifications You must be signed in to change notification settings

dsul/compare-sorting-algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

This is my playground to experiment with and analyze the performance (currently just the execution times) of various sorting algorithm implementations I have written in Python.

Uses the timeit library for measuring the execution time.

sorting_algorithms

This module contains all of the sorting algorithms used for comparisons. Each algorithm takes a single list of floats as the input so that the compare script can depend on that signature and treat them the same. Each implementation is simple to change to be mutating or non-mutating depending on your preference. That trade-off is not relevant to the testing I am doing, so most of them opt to mutate. Functions that begin with an underscore are helper functions and are ignored by the test script because of that naming convention.

The sorting algorithms currently implemented are:

  • Top-down mergesort with insertion sort optimization
  • Bottom-up mergesort
  • Insertion sort
  • Shellsort with Knuth's increment sequence (https://oeis.org/A003462)
  • Quicksort (two-way partition with simple pivot selection)

compare

This script uses the timeit library to compare the execution times of two sorting algorithms. It makes the assumption that the algorithms are contained in a sorting_algorithms module.

The call signature of the script is as follows:

./compare.py <sorting_function_name> <sorting_function_name> <number_of_trials> <list_length>

For example, executing ./compare.py top_down_mergesort insertion_sort 3 10000 produces the output:

Trials: 3
Input Length: 10000

Average Time:
top_down_mergesort: 0.016301
insertion_sort: 2.191806

top_down_mergesort ran 134.5 times faster than insertion_sort.

test

This script runs a simple test suite to ensure the algorithms are correctly sorting (only tests on a single, randomized data input for now). The script finds all callable, non-helper functions from the sorting_algorithms module and runs each one against the randomly generated array, or if provided a function name from sorting_modules, tests just that algorithm. This script was added to alleviate the drudgery of manually testing each algorithm in the python shell whenever I wanted or needed to make changes.

The call signature of the script is as follows:

./test.py [sorting_function_name]

For example, executing ./test.py with no argument produces the output:

image

Locally

To experiment locally, remember to give execute permissions to test.py and compare.py.

chmod +x ./test.py ./compare.py

About

Compare the execution times of sorting algorithm implementations in Python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages