Skip to main content

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

Metadata

Release files for ImpSort 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ImpSort 0.0.2
File Size Uploaded
ImpSort-0.0.2.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ImpSort 0.0.2
File Interpreter ABI Platform
ImpSort-0.0.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 19.0 kB

Release files / ImpSort-0.0.2.tar.gz

Download URL ImpSort-0.0.2.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
efc776c7518ad23bdc69c7b72ccaced9cbdd670d3f8bc4ae293e6a62f5d3e104
BLAKE2b-256 checksum
How to use checksums
20840193dddaa142138e8318a7a8677a2dce9d21efdd144797c3bed10e12bbd4
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / ImpSort-0.0.2-py2.py3-none-any.whl

Download URL ImpSort-0.0.2-py2.py3-none-any.whl
Size 8.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
28c1adaeb99ffc3a792f100ea865c60d50c98a8de7e876a45fee2af4857eeb17
BLAKE2b-256 checksum
How to use checksums
986c86fe8917a97c73520d8346942c7a25d1e28549c81f94c10291b9f4389f53
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page