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.

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

shansort-0.1.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

shansort-0.1.2-cp311-cp311-win_amd64.whl (60.3 kB view details)

Uploaded CPython 3.11Windows x86-64

File details

Details for the file shansort-0.1.2.tar.gz.

File metadata

  • Download URL: shansort-0.1.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for shansort-0.1.2.tar.gz
Algorithm Hash digest
SHA256 94b79d8f8a3983ed16b2592a3995369e842c1bd1e22a0502cc1c28899f512727
MD5 a38678bc71e48242f1a01d9e0c0b92fd
BLAKE2b-256 67ac5e3c6e519e41ad15db79d063044378d83506ce56e99f31f880e4779243fa

See more details on using hashes here.

File details

Details for the file shansort-0.1.2-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: shansort-0.1.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 60.3 kB
  • Tags: CPython 3.11, 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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b9a83ce955a35e347f7d073288bfe775cb831a500adb5aeb61371960b5ae9129
MD5 b5c3b5270c0ed28a67f1d345fcd1be26
BLAKE2b-256 dcfa76680acc25fd310724eb1844e94b1007b3ca6a38eee418f6fd9225f59333

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