Metadata-Version: 2.2
Name: adaptive-hybrid-sort
Version: 1.0.0
Summary: An enhanced adaptive sorting algorithm with pattern detection
Author-email: valEn1Ob0 <your.email@example.com>
License: MIT License
        
        Copyright (c) 2024 Valentin
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE. 
Project-URL: Homepage, https://github.com/valEn1Ob0/adaptive-hybrid-sort
Project-URL: Repository, https://github.com/valEn1Ob0/adaptive-hybrid-sort.git
Project-URL: Issues, https://github.com/valEn1Ob0/adaptive-hybrid-sort/issues
Keywords: sorting,algorithm,adaptive,pattern-detection,optimization
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.19.0
Requires-Dist: pandas>=1.2.0
Requires-Dist: matplotlib>=3.3.0
Requires-Dist: seaborn>=0.11.0
Requires-Dist: tqdm>=4.50.0
Requires-Dist: psutil>=5.7.0
Requires-Dist: memory_profiler>=0.58.0

# 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)

```bash
pip install adaptive-hybrid-sort
```

### From Source

```bash
# 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:
```bash
sorts setup
```

2. Sort a single array:
```bash
sorts sort --size 1000 --pattern random
```
Available patterns: `random`, `nearly_sorted`, `reversed`, `few_unique`, `sorted`, `pipeline`

3. Run benchmarks:
```bash
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

```python
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

```python
# 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

```python
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](LICENSE) file for details.

## Author

[valEn1Ob0](https://github.com/valEn1Ob0)

## Acknowledgments

- Inspired by TimSort's adaptive approach
- Uses NumPy for efficient array operations
- Parallel processing inspired by parallel merge sort 
