Adaptive Hybrid Sort
A novel sorting algorithm that combines multiple sorting strategies with pattern detection and parallel processing capabilities. This algorithm is particularly efficient for certain types of data patterns and provides stable sorting guarantees.
Features
- Pattern Detection: Automatically detects and adapts to different data patterns
- Parallel Processing: Utilizes multiple cores for large datasets
- Stable Sorting: Maintains relative order of equal elements
- Memory Efficient: Optimized memory usage with NumPy operations
- Type Preserving: Maintains input data types
- Fallback Mechanism: Gracefully handles edge cases
Performance Characteristics
The algorithm shows different performance characteristics for various input patterns:
| Pattern | Performance vs NumPy | Best Use Case |
|---|---|---|
| Reversed | 0.51x | Large reversed sequences |
| Random | 0.09x | General purpose sorting |
| Few Unique | 0.08x | Data with many duplicates |
| Nearly Sorted | 0.02x | Almost sorted data |
| Pipeline | 0.02x | Streaming data patterns |
Installation
From PyPI (Recommended)
pip install adaptive-hybrid-sort
From Source
# Clone the repository
git clone https://github.com/valEn1Ob0/adaptive-hybrid-sort.git
# Navigate to the directory
cd adaptive-hybrid-sort
# Install the package
pip install -e .
# Setup the environment
sorts setup
Usage
Command Line Interface
The package provides a convenient command-line interface through the sorts command:
- Setup environment and install requirements:
sorts setup
- Sort a single array:
sorts sort --size 1000 --pattern random
Available patterns: random, nearly_sorted, reversed, few_unique, sorted, pipeline
- Run benchmarks:
sorts benchmark --sizes 1000 2000 4000 --patterns random nearly_sorted --runs 3
The benchmark results will be saved in a timestamped directory under sorting_results/.
Python API
from adaptive_hybrid_sort import EnhancedAdaptiveSort
# Create a sorter instance
sorter = EnhancedAdaptiveSort()
# Sort an array
arr = [64, 34, 25, 12, 22, 11, 90]
sorted_arr = sorter.sort(arr)
Advanced Usage
# Custom thresholds for different array sizes
sorter = EnhancedAdaptiveSort(
threshold_small=32, # Threshold for insertion sort
threshold_chunk=2048 # Threshold for chunk size
)
# Sort different types of data
# Integers
int_arr = [1000, 1, 100, 10]
sorted_ints = sorter.sort(int_arr)
# Floats
float_arr = [3.14, 1.41, 2.71, 1.73]
sorted_floats = sorter.sort(float_arr)
Benchmarking
from adaptive_hybrid_sort import SortingVisualizer
# Create visualizer
visualizer = SortingVisualizer()
# Run benchmarks
sizes = [1000, 10000, 100000]
patterns = ["random", "nearly_sorted", "reversed", "few_unique", "sorted", "pipeline"]
results = visualizer.benchmark_sorting(sizes, patterns)
# Generate visualizations
visualizer.plot_performance_comparison(results)
Algorithm Details
1. Pattern Detection
The algorithm uses statistical sampling to detect patterns in the input data:
- Sorted sequences
- Reversed sequences
- Nearly sorted data
- Data with many duplicates
2. Sorting Strategies
Adapts its strategy based on input characteristics:
- Small Arrays: Optimized insertion sort
- Nearly Sorted: Direct insertion sort
- Many Duplicates: Three-way partitioning
- Large Arrays: Parallel merge sort
- Random Data: Hybrid quicksort
3. Optimizations
- Vectorized operations using NumPy
- Binary search for insertion points
- Adaptive chunk sizing for parallel processing
- Memory-efficient merging
- Cache-friendly operations
Performance Tips
-
Array Size
- Small arrays (< 32 elements): Uses insertion sort
- Medium arrays (32-2048 elements): Uses quicksort
- Large arrays (> 2048 elements): Uses pattern detection
-
Data Patterns
- Best performance on reversed sequences
- Efficient with duplicate elements
- Good for nearly sorted data
- Stable performance on random data
-
Memory Usage
- Uses about 2x input size for worst case
- More efficient for nearly sorted data
- Parallel processing requires additional memory
Contributing
Contributions are welcome! Here are some areas for improvement:
- SIMD optimization for parallel operations
- Additional pattern detection strategies
- GPU acceleration for large arrays
- Memory optimization for in-place operations
- Additional benchmarking scenarios
License
MIT License - see the LICENSE file for details.
Author
Acknowledgments
- Inspired by TimSort's adaptive approach
- Uses NumPy for efficient array operations
- Parallel processing inspired by parallel merge sort
Metadata
Release files for adaptive-hybrid-sort 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| adaptive_hybrid_sort-1.0.0.tar.gz | 15.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| adaptive_hybrid_sort-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.0 kB
Release files / adaptive_hybrid_sort-1.0.0.tar.gz
| Download URL | adaptive_hybrid_sort-1.0.0.tar.gz |
|---|---|
| Size | 15.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7d6579f008363b0d832fe99f641edd3594f60ed86e3f6eb5e1847ce11160d4c8
|
|
BLAKE2b-256 checksum How to use checksums |
96a090b10bc9aeca7e610caca80b65394daa0274f44bcf445e2425417cf03fea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / adaptive_hybrid_sort-1.0.0-py3-none-any.whl
| Download URL | adaptive_hybrid_sort-1.0.0-py3-none-any.whl |
|---|---|
| Size | 15.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ca5738ffbeecce513984df94b3823a2dfe81d138d61a96ec380b1e04bb4e82b1
|
|
BLAKE2b-256 checksum How to use checksums |
c25d353ca061a7c24b60fa97fc9d118f7796b40618c97f6ff6a40aaf8ce6cec2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Security for adaptive-hybrid-sort 1.0.0
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.