Skip to main content

An enhanced adaptive sorting algorithm with pattern detection

Project description

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:

  1. Setup environment and install requirements:
sorts setup
  1. Sort a single array:
sorts sort --size 1000 --pattern random

Available patterns: random, nearly_sorted, reversed, few_unique, sorted, pipeline

  1. 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

  1. Array Size

    • Small arrays (< 32 elements): Uses insertion sort
    • Medium arrays (32-2048 elements): Uses quicksort
    • Large arrays (> 2048 elements): Uses pattern detection
  2. Data Patterns

    • Best performance on reversed sequences
    • Efficient with duplicate elements
    • Good for nearly sorted data
    • Stable performance on random data
  3. 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:

  1. SIMD optimization for parallel operations
  2. Additional pattern detection strategies
  3. GPU acceleration for large arrays
  4. Memory optimization for in-place operations
  5. Additional benchmarking scenarios

License

MIT License - see the LICENSE file for details.

Author

valEn1Ob0

Acknowledgments

  • Inspired by TimSort's adaptive approach
  • Uses NumPy for efficient array operations
  • Parallel processing inspired by parallel merge 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 Distribution

adaptive_hybrid_sort-1.0.0.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

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

adaptive_hybrid_sort-1.0.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file adaptive_hybrid_sort-1.0.0.tar.gz.

File metadata

  • Download URL: adaptive_hybrid_sort-1.0.0.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for adaptive_hybrid_sort-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7d6579f008363b0d832fe99f641edd3594f60ed86e3f6eb5e1847ce11160d4c8
MD5 2a4274c3cf3b869b3b15e98edc95eca9
BLAKE2b-256 96a090b10bc9aeca7e610caca80b65394daa0274f44bcf445e2425417cf03fea

See more details on using hashes here.

File details

Details for the file adaptive_hybrid_sort-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for adaptive_hybrid_sort-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca5738ffbeecce513984df94b3823a2dfe81d138d61a96ec380b1e04bb4e82b1
MD5 15f8ac704f7ad567bd4b42cca8030c0f
BLAKE2b-256 c25d353ca061a7c24b60fa97fc9d118f7796b40618c97f6ff6a40aaf8ce6cec2

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