Skip to main content

A Python library that implements a sort algorithm based on randomization

Project description

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

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

random_sort-0.1.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

random_sort-0.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file random_sort-0.1.1.tar.gz.

File metadata

  • Download URL: random_sort-0.1.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for random_sort-0.1.1.tar.gz
Algorithm Hash digest
SHA256 21b7b2300be4b3bbb4e4930aa0c7f6e0d5ee074578f3cc68a181a718a4a7635e
MD5 0278533829761634b010a14aa7729ff7
BLAKE2b-256 c77717c1f7f86f8bbda0c6b92e5b1593f8252f5ae3a5626a03fa44d1c56b2867

See more details on using hashes here.

File details

Details for the file random_sort-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: random_sort-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for random_sort-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bb62c74aa8c2dbf6eeb1d8f652ddd08c92428362d49cdb9e4295e500e786aa69
MD5 27604044f6d41775a80311f91f70811c
BLAKE2b-256 599fbdeb5dbe4f1495f68d82d08614e25c2f4a796505aa96ca07ec4f198b37ee

See more details on using hashes here.

Supported by

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