Skip to main content

Unified fast radix sort for int, float, and string

Project description

shansort

Unified fast radix sort for int, float, and string in Python

shansort provides a blazing fast sorting function implemented in C++ with pybind11, using radix sort optimized for integers, floating-point numbers, and ASCII strings.

How It Works

shansort uses radix sort algorithms specialized for three data types:

  • Integers: Uses a 64-bit radix sort based on XOR with the sign bit to handle negative numbers correctly.
  • Floats: Converts floats to sortable 64-bit keys via bit manipulations that preserve the order, then applies radix sort.
  • Strings: Performs LSD (least significant digit) radix sort on ASCII characters from the end of the strings, handling variable lengths efficiently.

Before sorting, the algorithm checks if the data is already sorted ascending or descending to optimize performance.

This approach achieves high speed by avoiding comparison-based sorting and leveraging bitwise operations and counting sort passes.

Features

  • Fast radix sort specialized for int64, double, and ASCII strings
  • Handles sorted, reverse sorted, and unsorted data efficiently
  • Python interface: shansort.sort(list) supports int, float, or string lists
  • Up to 3.7x to 5.3x faster than Python built-in sorted() on large datasets

Installation

pip install shansort


import shansort

# Sort integers
ints = [5, 3, 9, 1]
print(shansort.sort(ints))  # [1, 3, 5, 9]

# Sort floats
floats = [3.14, 2.71, 1.62, 0.0]
print(shansort.sort(floats))  # [0.0, 1.62, 2.71, 3.14]

# Sort strings (ASCII only)
strings = ["banana", "apple", "cherry"]
print(shansort.sort(strings))  # ['apple', 'banana', 'cherry']


## Metrics
Benchmarking with 10,000,000 elements on a typical modern CPU:
| Data Type | Python built-in `sorted()` | `shansort.sort()` | Speedup Factor (times faster) |
| --------- | -------------------------- | ----------------- | ----------------------------- |
| Integers  | 9.78 seconds               | 1.84 seconds      | \~5.3x                        |
| Floats    | 8.82 seconds               | 1.98 seconds      | \~4.5x                        |
| Strings   | 15.60 seconds              | 4.25 seconds      | \~3.7x                        |




Algorithm Limits & Data Ranges
Supports 64-bit signed integers (int64_t) and 64-bit doubles (double).

Sorts ASCII strings only (no Unicode).

Uses 8 passes of 8 bits each (64-bit radix), so limited to 64-bit data.

Works correctly and efficiently within these ranges.



## Test Code

import random
import time
import shansort

def benchmark_sort():
    N = 10**7

    # Generate random data once, reuse for all runs for consistency
    data_int = [random.randint(-1_000_000, 1_000_000) for _ in range(N)]
    data_float = [random.uniform(-1e5, 1e5) for _ in range(N)]
    data_str = [''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=10)) for _ in range(N)]

    for dtype, data in [('int', data_int), ('float', data_float), ('string', data_str)]:
        builtin_times = []
        custom_times = []
        correctness_passes = 0

        print(f"\nBenchmarking {dtype} sort with {N:,} elements over 20 runs:")

        for run in range(5):
            # Built-in sort
            builtin_data = list(data)
            t0 = time.time()
            builtin_sorted = sorted(builtin_data)
            t1 = time.time()

            # Custom C++ sort
            custom_data = list(data)
            t2 = time.time()
            custom_sorted = shansort.sort(custom_data)
            t3 = time.time()

            # Store timings
            builtin_times.append(t1 - t0)
            custom_times.append(t3 - t2)

            # Verify correctness
            if builtin_sorted == custom_sorted:
                correctness_passes += 1
            else:
                print(f"Run {run+1}: ❌ Sort correctness FAIL")

        print(f"Built-in sorted() average time: {sum(builtin_times)/5:.2f} seconds")
        print(f"C++ fast_sort() average time: {sum(custom_times)/5:.2f} seconds")
        print(f"Correctness passed in {correctness_passes}/5 runs")

benchmark_sort()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

shansort-0.1.4-cp313-cp313-win_amd64.whl (61.6 kB view details)

Uploaded CPython 3.13Windows x86-64

shansort-0.1.4-cp313-cp313-win32.whl (58.2 kB view details)

Uploaded CPython 3.13Windows x86

shansort-0.1.4-cp312-cp312-win_amd64.whl (61.6 kB view details)

Uploaded CPython 3.12Windows x86-64

shansort-0.1.4-cp312-cp312-win32.whl (58.2 kB view details)

Uploaded CPython 3.12Windows x86

File details

Details for the file shansort-0.1.4-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: shansort-0.1.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 61.6 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for shansort-0.1.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 34676a998a3e4e793234829db155a224058dd140a9fc2e82c851948e76e620b1
MD5 bcd4339f52f394999c3c9a661813b443
BLAKE2b-256 de4be6d108e354a061738823035f7639ed026ff27e6fb40d90ebd7731a16fc1d

See more details on using hashes here.

File details

Details for the file shansort-0.1.4-cp313-cp313-win32.whl.

File metadata

  • Download URL: shansort-0.1.4-cp313-cp313-win32.whl
  • Upload date:
  • Size: 58.2 kB
  • Tags: CPython 3.13, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for shansort-0.1.4-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 b68276d528b30fa2f803aa1cec92280c6d60df680a7b748d91f93582a1821023
MD5 c313ecdae539f60e0228c050821f3145
BLAKE2b-256 fe44c894f2005a84f2594d2d98cf79aa332a3e535446d504ee0fff6d9b4ca991

See more details on using hashes here.

File details

Details for the file shansort-0.1.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: shansort-0.1.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 61.6 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for shansort-0.1.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5d279ab3aed93d22d0f125a2d832681de943da876e223d41410d8539354f569c
MD5 6c71fa11751b471460ab7e4f0ed9d7e5
BLAKE2b-256 46572234f6894fc3692d975f27e5362e1fb0e4e25fe86d42f7440044cb29423e

See more details on using hashes here.

File details

Details for the file shansort-0.1.4-cp312-cp312-win32.whl.

File metadata

  • Download URL: shansort-0.1.4-cp312-cp312-win32.whl
  • Upload date:
  • Size: 58.2 kB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for shansort-0.1.4-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 160e72dae4623ccdc807cfab74e197fa526a87e8bf667caeea78e05462b8792e
MD5 c96be4c2e5263a8072fb1d727ec06b7a
BLAKE2b-256 01f7dfc4902fd61ba43f4f195149a6c40b27fcd2655c42d19f36bf977d289157

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