Skip to main content

Random Sort

A Python library that implements an "innovative" sorting algorithm based on randomization.

GitHub License PyPI Version

Description

Random Sort implements the classic "Bogosort" algorithm as a joke/educational library. It repeatedly shuffles a list randomly until it happens to be sorted. This is of course extremely inefficient (with an average case of O(n × n!)) and is intended for fun and education about algorithm efficiency, not for actual use.

Installation

# From PyPI (when published)
pip install random_sort

# Directly from GitHub
pip install git+https://github.com/FrancescoGrazioso/random_sort.git

Usage

from random_sort import random_sort, bogosort

# Create an unsorted list
my_list = [3, 1, 4, 1, 5, 9, 2, 6]

# Sort the list with random_sort
sorted_list = random_sort(my_list)

print(sorted_list)  # [1, 1, 2, 3, 4, 5, 6, 9]

# You can also use the alias 'bogosort'
sorted_list = bogosort(my_list)

# Control the maximum number of attempts
sorted_list = random_sort(my_list, max_attempts=50)

# Get verbose output
sorted_list = random_sort(my_list, verbose=True)

# Sort using a key function (like the built-in sort)
people = [
    {"name": "Alice", "age": 30},
    {"name": "Bob", "age": 25},
    {"name": "Charlie", "age": 35}
]
sorted_people = random_sort(people, key=lambda x: x["age"])

How it works

The random_sort algorithm uses a revolutionary approach to sorting:

  1. Completely randomize the list
  2. Check if it happens to be sorted
  3. If not sorted, repeat from step 1 until you get a sorted list
  4. If it takes too many attempts, it falls back to the built-in sorting algorithm

This algorithm has a time complexity of O(∞) in the worst case theoretically, but in practice it's limited by the max_attempts parameter to avoid infinite recursion.

Features

  • Implements the classic "Bogosort" algorithm
  • Prevents infinite recursion with configurable max attempts
  • Provides verbose mode to see the sorting progress
  • Supports key functions just like Python's built-in sort
  • Early termination for lists that are too large
  • 100% test coverage

Performance

Below is a table showing the expected number of attempts needed to sort lists of different sizes:

List Size Expected Attempts
1 1
2 2
3 6
4 24
5 120
6 720
7 5,040
8 40,320
9 362,880
10 3,628,800

As you can see, the number of attempts grows factorially with the list size!

Warning

This library was created for humorous and educational purposes. Don't use it in production environments, unless you have infinite time at your disposal and would like to use it creatively.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

Metadata

Release files for random-sort 0.1.1

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

Source distribution (sdist)

Source distribution for random-sort 0.1.1
File Size Uploaded Provenance Actions
random_sort-0.1.1.tar.gz 5.0 kB Without provenance Details for random_sort-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for random-sort 0.1.1
File Interpreter ABI Platform Actions
random_sort-0.1.1-py3-none-any.whl Python 3 none any Details for random_sort-0.1.1-py3-none-any.whl

Total release size: 10.4 kB

Release files / random_sort-0.1.1.tar.gz

Download URL random_sort-0.1.1.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
21b7b2300be4b3bbb4e4930aa0c7f6e0d5ee074578f3cc68a181a718a4a7635e
BLAKE2b-256 checksum
How to use checksums
c77717c1f7f86f8bbda0c6b92e5b1593f8252f5ae3a5626a03fa44d1c56b2867
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.6

Release files / random_sort-0.1.1-py3-none-any.whl

Download URL random_sort-0.1.1-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb62c74aa8c2dbf6eeb1d8f652ddd08c92428362d49cdb9e4295e500e786aa69
BLAKE2b-256 checksum
How to use checksums
599fbdeb5dbe4f1495f68d82d08614e25c2f4a796505aa96ca07ec4f198b37ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.6

Security for random-sort 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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