| Crates.io | tilesort |
| lib.rs | tilesort |
| version | 0.2.0 |
| created_at | 2025-11-10 02:12:50.854748+00 |
| updated_at | 2025-11-18 04:34:18.682118+00 |
| description | A sorting algorithm optimized for datasets with pre-sorted contiguous blocks (tiles) |
| homepage | https://github.com/evanjpw/tilesort |
| repository | https://github.com/evanjpw/tilesort |
| max_upload_size | |
| id | 1924658 |
| size | 215,009 |
A sorting algorithm optimized for datasets with pre-sorted contiguous blocks (tiles).
Tilesort is a specialized sorting algorithm that achieves high performance when your data consists of non-overlapping, pre-sorted contiguous blocks. Instead of sorting individual elements, tilesort identifies these "tiles" and arranges them as discrete units.
Tilesort is particularly effective when:
Do not use tilesort if:
Tilesort is a specialized algorithm for a specific data pattern. If you're unsure whether your data has pre-sorted tiles, use a standard sorting algorithm.
For a dataset of n elements partitioned into k tiles:
The performance is primarily determined by k (number of tiles) rather than n (total elements), making it highly efficient when the number of tiles is much smaller than the total number of elements.
pip install tilesort
Requirements: Python 3.8-3.14
Add to your Cargo.toml:
[dependencies]
tilesort = "0.1.0"
The Python API mirrors Python's built-in list.sort() and sorted() functions:
import tilesort
# Sort a list in place (like list.sort())
data = [3, 4, 5, 1, 2, 6, 7, 8]
tilesort.sort(data)
print(data) # [1, 2, 3, 4, 5, 6, 7, 8]
# Return a sorted copy (like sorted())
data = [3, 4, 5, 1, 2, 6, 7, 8]
sorted_data = tilesort.sorted(data)
print(sorted_data) # [1, 2, 3, 4, 5, 6, 7, 8]
print(data) # [3, 4, 5, 1, 2, 6, 7, 8] (unchanged)
# Sort with a key function
words = ["elephant", "cat", "dog", "a", "bear"]
tilesort.sort(words, key=len)
print(words) # ["a", "cat", "dog", "bear", "elephant"]
# Sort in reverse order
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
tilesort.sort(numbers, reverse=True)
print(numbers) # [9, 6, 5, 4, 3, 2, 1, 1]
# Combine key and reverse
data = [-5, -3, -1, 2, 4]
tilesort.sort(data, key=abs, reverse=True)
print(data) # [-5, 4, -3, 2, -1]
# Sort custom objects
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
people = [Person("Alice", 30), Person("Bob", 25), Person("Charlie", 35)]
tilesort.sort(people, key=lambda p: p.age)
# Now sorted by age: Bob (25), Alice (30), Charlie (35)
use tilesort::{tilesort, tilesorted, tilesort_by_key, tilesort_reverse};
fn main() {
// Sort in place
let mut data = vec![3, 4, 5, 1, 2, 6, 7, 8];
tilesort(&mut data);
println!("{:?}", data); // [1, 2, 3, 4, 5, 6, 7, 8]
// Return a sorted copy
let data = vec![3, 4, 5, 1, 2, 6, 7, 8];
let sorted = tilesorted(&data);
println!("{:?}", sorted); // [1, 2, 3, 4, 5, 6, 7, 8]
println!("{:?}", data); // [3, 4, 5, 1, 2, 6, 7, 8] (unchanged)
// Sort in reverse
let mut data = vec![3, 1, 4, 1, 5, 9, 2, 6];
tilesort_reverse(&mut data);
println!("{:?}", data); // [9, 6, 5, 4, 3, 2, 1, 1]
// Sort with a key function
let mut data = vec![-5i32, -3, -1, 2, 4];
tilesort_by_key(&mut data, |&x| x.abs());
println!("{:?}", data); // [-1, 2, -3, 4, -5]
// Sort strings by length
let mut words = vec!["elephant", "cat", "dog", "a", "bear"];
tilesort_by_key(&mut words, |s| s.len());
println!("{:?}", words); // ["a", "cat", "dog", "bear", "elephant"]
// Sort custom structs
#[derive(Clone)]
struct Person {
name: String,
age: u32,
}
let mut people = vec![
Person { name: "Alice".to_string(), age: 30 },
Person { name: "Bob".to_string(), age: 25 },
Person { name: "Charlie".to_string(), age: 35 },
];
tilesort_by_key(&mut people, |p| p.age);
// Now sorted by age: Bob (25), Alice (30), Charlie (35)
}
Tilesort operates in two phases:
The algorithm automatically detects tile boundaries by scanning for order violations. When elements are out of order, a new tile begins. The tiles are then sorted based on their key ranges and concatenated to produce the final sorted sequence.
Given input: [3, 4, 5, 1, 2, 6, 7, 8]
Scan identifies three tiles:
[3, 4, 5] (range 3-5)[1, 2] (range 1-2)[6, 7, 8] (range 6-8)Tiles are sorted by their ranges: Tile 1, Tile 0, Tile 2
Output: [1, 2, 3, 4, 5, 6, 7, 8]
tilesort.sort(list, *, key=None, reverse=False) - Sort a list in placetilesort.sorted(list, *, key=None, reverse=False) - Return a sorted copyBoth functions support:
key: Optional function to extract comparison key from each elementreverse: If True, sort in descending orderIn-place sorting:
tilesort(data: &mut [T]) - Sort in ascending ordertilesort_reverse(data: &mut [T]) - Sort in descending ordertilesort_by_key(data: &mut [T], key_fn: F) - Sort by custom keytilesort_by_key_reverse(data: &mut [T], key_fn: F) - Sort by custom key, descendingCopying variants:
tilesorted(data: &[T]) -> Vec<T> - Return sorted copytilesorted_reverse(data: &[T]) -> Vec<T> - Return sorted copy, descendingtilesorted_by_key(data: &[T], key_fn: F) -> Vec<T> - Return sorted copy by keytilesorted_by_key_reverse(data: &[T], key_fn: F) -> Vec<T> - Return sorted copy by key, descendingAll functions work with any type T that implements Ord + Clone. Key functions must return a type K that implements
Ord.
# Run tests
cargo test
# Build the library
cargo build --release
# Generate documentation
cargo doc --open
Requirements:
# Install development dependencies
uv sync --group dev
# Build and install in development mode
maturin develop --features python
# Run Python tests
just test-python
# or: uv run --group dev pytest python/tests/
# Run type checking
just typecheck
# or: uv run --group dev mypy python/
# Run all tests (Rust + Python)
just test
# Run linter
just lint
# Format code
just format
This project uses Just as a command runner:
just # List all available commands
just test # Run all tests (Rust + Python)
just test-rust # Run Rust tests only
just test-python # Run Python tests only
just typecheck # Run mypy type checking
just lint # Run ruff linter
just format # Format code with ruff
just build # Build Python package
just bench # Run benchmarks
just check # Run all checks (test + typecheck + lint)
just clean # Clean build artifacts
Performance benchmarks compare tilesort against Rust's standard sort across different scenarios:
# Run all benchmarks
cargo bench
# Run specific benchmark group
cargo bench uniform_tiles
cargo bench varied_tiles
cargo bench hybrid_tiles
cargo bench random_data
cargo bench key_function
cargo bench realistic_workload
Benchmark scenarios:
Results are saved to target/criterion/ with HTML reports.
Licensed under either of:
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.
See CHANGELOG.md for a list of changes in each release.