A C++20 implementation of TimSort, a stable O(n log n) sorting algorithm optimized for partially-sorted data.
cpp-TimSort is a C++ library implementing the TimSort algorithm, a hybrid stable sorting algorithm derived from Python's and OpenJDK's implementations. It provides efficient O(n log n) sorting that performs exceptionally well on partially-sorted data, offering both `timsort` and `timmerge` functions as drop-in replacements for their C++ standard library counterparts.
C++ developers working with datasets that have inherent partial ordering, such as log files, time-series data, or any collections where elements are likely already somewhat sorted.
Developers choose cpp-TimSort when they need stable sorting with better performance on real-world data patterns than standard library algorithms, particularly for complex data types and partially ordered sequences, without requiring extra heap memory.
A C++ implementation of timsort
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Benchmarks show timsort is up to 10x faster on reversed sequences and significantly quicker on sorted data compared to std::stable_sort, making it ideal for real-world datasets with inherent structure.
Maintains the relative order of equal elements, which is crucial for multi-key sorting operations, as highlighted in the key features.
Unlike std::ranges::stable_sort, it doesn't rely on extra heap memory for fallbacks, providing more predictable performance in memory-constrained environments.
gfx::timmerge demonstrates better performance than std::inplace_merge when handling complex data types like std::string, as evidenced by benchmark results showing faster times on sparsely overlapping subranges.
On randomized sequences, timsort is slower than std::sort, as admitted in the README, making it a poor choice for datasets without any pre-existing order.
The latest version requires C++20, and support for older standards is relegated to unmaintained branches, forcing upgrades or limiting adoption in legacy projects.
It cannot fall back to alternative algorithms when extra heap memory is unavailable, unlike std::ranges::stable_sort, which could lead to suboptimal behavior in low-memory situations.