Skip to main content

A python library for interacting with some very terrible sorting algorithms

Project description

ImpSort

Improbably bad sorting for lists in Python.

Why?

There was a conversation at a coffee shop about how terrible Bogo Sort is, so the question became can we use that concept to make even worse algorithms.

What?

This library has implementations of some terrible sorting algorithms with some added "niceties" such as:

  • Keeping track of metadata
  • Having iterable (generator) versions of the algorithms
  • Having equally terrible comparisons
  • Timeouts for sanity's sake

Algorithms

These are the algorithms currently implemented in ImpSort.

Bogo Sort

This is the sorting algorithm that started this whole project.

It works by randomly re-sorting the entire list then checking if the new order is sorted.

Random Swap Sort

This is like Bogo Sort... but worse.

It works by randomly picking two items in the list, then swapping them. It then checks to see if the whole list is sorted. It is also able to pick the same item, which would result in no change to the list at all!

Recompile Sort

This is based on the old adage that states that if your code doesn't compile on the first try, it might on the next try....

So basically this goes through the whole list and sets each item to the same value it had, then checks if the list is now sorted. I believe Einstein would call this pure madness.

Stalin Sort

This is a sorting algorithm that always completes in one step. That is that it operates at O(N).

It works by iterating through each item in the list. If an item is not in order, it is eliminated. The end result is guaranteed to be sorted, but you are not guaranteed to still have all the same items you started with...

Installation

Use pip to install:

pip install ImpSort

Usage

Basic usage:

# ImpSort objects are used to perform sorting operations
# TimeOutException can be used to detect timeouts
from ImpSort import (ImpSort, TimeOutException)

# The default algorithm is 'rand_swap'
# You can alternatively set it as 'bogo' or 'recompile'
# You can also provide a random to use (such as a random.SystemRandom instance)
sorter = ImpSort()

unsorted_list = [24, 106, 2, 42]  # This is a sample unsorted list

# To simply sort the list:
sorter.sort(unsorted_list)

# To get metadata about the most recent run, use the meta attribute:
print(sorter.meta)

# To get each iteration as the sort progresses, use the sort_generator method to get an iterable
for u in sorter.sort_generator(unsorted_list):
    print(u)

# To set a timeout, use the timeout argument:
try:
    # This will time out if it hasn't returned in 10 seconds
    sorter.sort(unsorted_list, timeout=10)
except TimeOutException as e:
    # You can catch a TimeOutException
    print(e)  # The exception includes some statistics about the failed run

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

ImpSort-0.0.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

ImpSort-0.0.2-py2.py3-none-any.whl (8.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ImpSort-0.0.2.tar.gz.

File metadata

  • Download URL: ImpSort-0.0.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.0

File hashes

Hashes for ImpSort-0.0.2.tar.gz
Algorithm Hash digest
SHA256 efc776c7518ad23bdc69c7b72ccaced9cbdd670d3f8bc4ae293e6a62f5d3e104
MD5 3b72651df12a3927eb19e8b78872ab97
BLAKE2b-256 20840193dddaa142138e8318a7a8677a2dce9d21efdd144797c3bed10e12bbd4

See more details on using hashes here.

File details

Details for the file ImpSort-0.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: ImpSort-0.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.0

File hashes

Hashes for ImpSort-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 28c1adaeb99ffc3a792f100ea865c60d50c98a8de7e876a45fee2af4857eeb17
MD5 0ea09e697d31c67448f45999296de9e5
BLAKE2b-256 986c86fe8917a97c73520d8346942c7a25d1e28549c81f94c10291b9f4389f53

See more details on using hashes here.

Supported by

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