{"id":15047524,"url":"https://github.com/morwenn/cpp-sort","last_synced_at":"2026-01-18T09:01:39.848Z","repository":{"id":35765718,"uuid":"40045203","full_name":"Morwenn/cpp-sort","owner":"Morwenn","description":"Sorting algorithms \u0026 related tools for C++14","archived":false,"fork":false,"pushed_at":"2025-03-31T08:35:26.000Z","size":5364,"stargazers_count":639,"open_issues_count":39,"forks_count":58,"subscribers_count":33,"default_branch":"1.x.y-develop","last_synced_at":"2025-04-07T16:14:06.548Z","etag":null,"topics":["algorithm","cpp","cpp14","sorting"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Morwenn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-08-01T11:58:35.000Z","updated_at":"2025-03-31T08:35:31.000Z","dependencies_parsed_at":"2023-02-16T02:00:34.030Z","dependency_job_id":"36e5e57b-a666-45c9-890a-6eaa9b383c1b","html_url":"https://github.com/Morwenn/cpp-sort","commit_stats":{"total_commits":1625,"total_committers":3,"mean_commits":541.6666666666666,"dds":0.002461538461538515,"last_synced_commit":"4e7321d9cd67c81cf735746a4087ed89d19783ba"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morwenn%2Fcpp-sort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morwenn%2Fcpp-sort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morwenn%2Fcpp-sort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morwenn%2Fcpp-sort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Morwenn","download_url":"https://codeload.github.com/Morwenn/cpp-sort/tar.gz/refs/heads/1.x.y-develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248978261,"owners_count":21192747,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","cpp","cpp14","sorting"],"created_at":"2024-09-24T20:59:43.845Z","updated_at":"2026-01-18T09:01:39.753Z","avatar_url":"https://github.com/Morwenn.png","language":"C++","readme":"![cpp-sort logo](docs/images/cpp-sort-logo.svg)\n\n[![Latest Release](https://img.shields.io/badge/release-1.16.0-blue.svg)](https://github.com/Morwenn/cpp-sort/releases/tag/1.16.0)\n[![Conan Package](https://img.shields.io/badge/conan-cpp--sort%2F1.16.0-blue.svg)](https://conan.io/center/recipes/cpp-sort?version=1.16.0)\n[![Code Coverage](https://codecov.io/gh/Morwenn/cpp-sort/branch/develop/graph/badge.svg)](https://codecov.io/gh/Morwenn/cpp-sort)\n[![Pitchfork Layout](https://img.shields.io/badge/standard-PFL-orange.svg)](https://github.com/vector-of-bool/pitchfork)\n\n\u003e *It would be nice if only one or two of the sorting methods would dominate all of the others,\n\u003e regardless of application or the computer being used. But in fact, each method has its own\n\u003e peculiar virtues. [...] Thus we find that nearly all of the algorithms deserve to be remembered,\n\u003e since there are some applications in which they turn out to be best.*\n\u003e — Donald Knuth, The Art Of Computer Programming, Volume 3\n\n**cpp-sort** is a generic C++14 header-only sorting library. It revolves\naround one main generic sorting interface and provides several small tools\nto pick and/or design sorting algorithms. Using its basic sorting features\nshould be trivial enough:\n\n```cpp\n#include \u003carray\u003e\n#include \u003ciostream\u003e\n#include \u003ccpp-sort/sorters/smooth_sorter.h\u003e\n\nint main()\n{\n    std::array\u003cint, 5\u003e arr = { 5, 8, 3, 2, 9 };\n    cppsort::smooth_sort(arr);\n\n    // prints 2 3 5 8 9\n    for (int val: arr) {\n        std::cout \u003c\u003c val \u003c\u003c ' ';\n    }\n}\n```\n\n# The main features \u0026 the extra features\n\n**cpp-sort** provides a full set of sorting-related features. Here are the main building blocks\nof the library:\n* Every sorting algorithm exists as a function object called a [sorter](https://github.com/Morwenn/cpp-sort/wiki/Sorters)\n* Sorters can be wrapped in [sorter adapters](https://github.com/Morwenn/cpp-sort/wiki/Sorter-adapters) to augment their behaviour\n* The library provides a [sorter facade](https://github.com/Morwenn/cpp-sort/wiki/Sorter-facade) to easily build sorters\n* [Fixed-size sorters](https://github.com/Morwenn/cpp-sort/wiki/Fixed-size-sorters) can be used to efficiently sort tiny fixed-size collections\n* [Measures of presortedness](https://github.com/Morwenn/cpp-sort/wiki/Measures-of-presortedness) can be used to evaluate the disorder in a collection\n\nHere is a more complete example of what can be done with the library:\n\n```cpp\n#include \u003calgorithm\u003e\n#include \u003ccassert\u003e\n#include \u003cforward_list\u003e\n#include \u003cfunctional\u003e\n#include \u003cvector\u003e\n#include \u003ccpp-sort/adapters.h\u003e\n#include \u003ccpp-sort/sorters.h\u003e\n\nint main()\n{\n    struct wrapper { int value; };\n\n    std::forward_list\u003cwrapper\u003e li = { {5}, {8}, {3}, {2}, {9} };\n    std::vector\u003cwrapper\u003e vec = { {5}, {8}, {3}, {2}, {9} };\n\n    // When used, this sorter will use a pattern-defeating quicksort\n    // to sort random-access collections, and a mergesort otherwise\n    cppsort::hybrid_adapter\u003c\n        cppsort::pdq_sorter,\n        cppsort::merge_sorter\n    \u003e sorter;\n\n    // Sort li and vec in reverse order using their value member\n    sorter(li, std::greater\u003c\u003e{}, \u0026wrapper::value);\n    sorter(vec, std::greater\u003c\u003e{}, \u0026wrapper::value);\n\n    assert(std::equal(\n        li.begin(), li.end(),\n        vec.begin(), vec.end(),\n        [](const auto\u0026 lhs, const auto\u0026 rhs) { return lhs.value == rhs.value; }\n    ));\n}\n```\n\nEven when the sorting functions are used without the extra features, they still provide\nsome interesting guarantees (ideas often taken from the Ranges TS):\n* They provide both an iterator and a range interface\n* When possible, they accept a custom comparator parameter\n* Most of them accept a projection parameter\n* They correctly handle proxy iterators with `iter_swap` and `iter_move`\n* They also work when iterators don't provide post-incrementation nor post-decrementation\n* The value types of the collections to be sorted need not be default-constructible\n* The value types of the collections to be sorted need not be copyable (only movable)\n* Stateless sorters can be converted to a function pointer for each overloaded `operator()`\n* Sorters are function objects: they can directly be passed as \"overload sets\" to other functions\n\nYou can read more about all the available tools and find some tutorials about using\nand extending **cpp-sort** in [the wiki](https://github.com/Morwenn/cpp-sort/wiki).\n\n# Benchmarks\n\nThe following graph has been generated with a script found in the benchmarks\ndirectory. It shows the time needed for [`heap_sort`][heap-sorter] to sort one\nmillion elements without being adapted, then when it is adapted with either\n[`drop_merge_adapter`][drop-merge-adapter] or [`split_adapter`][split-adapter].\n\n![Graph showing the speed difference between heap_sort raw, then adapted with\nsplit_adapter and drop_merge_adapter, when the number of inversions in the\nstd::vector\u003cint\u003e to sort increases](https://i.imgur.com/IcjUkYF.png)\n\nAs can be seen above, wrapping `heap_sort` with either of the adapters makes it\n[*adaptive*][adaptive-sort] to the number of inversions in a non-intrusive\nmanner. The algorithms used to adapt it have different pros and cons, it is up\nto you to use either.\n\nThis benchmark is mostly there to show the possibilities offered by the\nlibrary. You can find more such commented benchmarks in the [dedicated wiki\npage][benchmarks].\n\n# Compiler support \u0026 tooling\n\n![Ubuntu builds status](https://github.com/Morwenn/cpp-sort/workflows/Ubuntu%20Builds/badge.svg?branch=develop)\n![Windows builds status](https://github.com/Morwenn/cpp-sort/workflows/MSVC%20Builds/badge.svg?branch=develop)\n![MacOS builds status](https://github.com/Morwenn/cpp-sort/workflows/MacOS%20Builds/badge.svg?branch=develop)\n\n**cpp-sort** requires C++14 support, and should work with the following compilers:\n* g++7 or more recent.\n* clang++6.0 or more recent (with both libstdc++ and libc++).\n* The versions of MinGW-w64 and AppleClang equivalent to the compilers mentioned above.\n* Visual Studio 2019 version 16.8.3 or more recent, only with `/permissive-`. A few features are unavailable.\n* clang-cl corresponding the the Visual Studio version above.\n\nThe compilers listed above are the ones used by the CI pipeline, and the library is also tested\nwith the most recent versions of those compilers on a regular basis. All the other compiler\nversions in-between are untested, but should also work. Feel free to open an issue if it isn't the\ncase.\n\nThe features in the library might differ depending on the C++ version used and on the compiler\nextensions enabled. Those changes are documented [in the wiki][changelog].\n\nThe main repository contains additional support for standard tooling such as CMake or Conan.\nYou can read more about those [in the wiki][tooling].\n\n# Thanks\n\n\u003e *I got a new car. I just need to put it together. They’re easier to steal piece by\n\u003e piece.*\n\u003e — Jarod Kintz, $3.33\n\nEven though some parts of the library are [original research](https://github.com/Morwenn/cpp-sort/wiki/Original-research)\nand some others correspond to custom and rather naive implementations of standard\nsorting algorithms, **cpp-sort** also reuses a great deal of code and ideas from\nopen-source projects, often altered to integrate seamlessly into the library. Here\nis a list of the external resources used to create this library. I hope that the\nmany different licenses are compatible. If it is not the case, please contact me\n(or submit an issue) and we will see what can be done about it:\n\n* Some of the algorithms used by `insertion_sorter` and `pdq_sorter` come from\nOrson Peters' [pattern-defeating quicksort](https://github.com/orlp/pdqsort). Some\nparts of the benchmarks come from there as well.\n\n* The algorithm used by `tim_sorter` comes from Goro Fuji's (gfx) [implementation\nof a Timsort](https://github.com/gfx/cpp-TimSort).\n\n* The three algorithms used by `spread_sorter` come from Steven Ross [Boost.Sort\nmodule](https://www.boost.org/doc/libs/1_80_0/libs/sort/doc/html/index.html).\n\n* The algorithm used by `d_ary_spread_sorter` comes from Tim Blechmann's\n[Boost.Heap module](https://www.boost.org/doc/libs/1_80_0/doc/html/heap.html).\n\n* The algorithm used by `spin_sorter` comes from the eponymous algorithm implemented\nin [Boost.Sort](https://www.boost.org/doc/libs/1_80_0/libs/sort/doc/html/index.html).\nby Francisco Jose Tapia.\n\n* [`utility::as_function`](https://github.com/Morwenn/cpp-sort/wiki/Miscellaneous-utilities#as_function),\n[`utility::static_const`](https://github.com/Morwenn/cpp-sort/wiki/Miscellaneous-utilities#static_const),\nand several projection-enhanced helper algorithms come from Eric Niebler's [Range\nv3](https://github.com/ericniebler/range-v3) library. Several ideas such as proxy\niterators, customization points and projections, as well as a few other utility\nfunctions also come from that library or from the related articles and standard\nC++ proposals.\n\n* The algorithm used by `ska_sorter` comes from Malte Skarupke's [implementation](https://github.com/skarupke/ska_sort)\nof his own [ska_sort](https://probablydance.com/2016/12/27/i-wrote-a-faster-sorting-algorithm/) algorithm.\n\n* The algorithm used by `drop_merge_sorter` comes from Adrian Wielgosik [C++\nreimplementation](https://github.com/adrian17/cpp-drop-merge-sort) of Emil Ernerfeldt's\n[drop-merge sort](https://github.com/emilk/drop-merge-sort).\n\n* Many enhanced standard algorithms are directly adapted from their counterparts\nin [libc++](https://libcxx.llvm.org/), enhanced to handle both projections and\nproxy iterators.\n\n* The library internally uses an `inplace_merge` function that works with forward\niterators. Its implementation uses a merge algorithm proposed by Dudziński and Dydek,\nand implemented by Alexander Stepanov and Paul McJones in their book [*Elements of\nProgramming*](http://www.elementsofprogramming.com/).\n\n* The `inplace_merge` overload for random-access iterators uses the *Symmerge* algorithm\nproposed by Pok-Son Kim and Arne Kutzner in [*Stable Minimum Storage Merging by Symmetric\nComparisons*](https://pdfs.semanticscholar.org/d664/cee462cb8e6a8ae2a1a7c6bab1b5f81e0618.pdf)\nwhen there isn't enough memory available to perform an out-of-place merge.\n\n* The implementation of Dijkstra's smoothsort used by `smooth_sorter` has been\ndirectly adapted from [Keith Schwarz's implementation](http://www.keithschwarz.com/interesting/code/?dir=smoothsort)\nof the algorithm.\n\n* The algorithm used by `wiki_sorter` has been adapted from BonzaiThePenguin's\n[WikiSort](https://github.com/BonzaiThePenguin/WikiSort).\n\n* The algorithm used by `grail_sorter` has been adapted from Mrrl's\n[GrailSort](https://github.com/Mrrl/GrailSort).\n\n* The algorithm used by `indirect_adapter` with forward or bidirectional iterators is a\nslightly modified version of Matthew Bentley's [indiesort](https://github.com/mattreecebentley/plf_indiesort).\n\n* The implementation of the random-access overload of `nth_element` used by some of the algorithms\ncomes from Danila Kutenin's [miniselect library](https://github.com/danlark1/miniselect) and uses\nAndrei Alexandrescu's [*AdaptiveQuickselect*](https://arxiv.org/abs/1606.00484) algorithm.\n\n* The sorting networks used by `sorting_network_sorter` all come [from this list](https://bertdobbelaere.github.io/sorting_networks_extended.html)\nmaintained by Bert Dobbelaere. The page has references to the sources of all of the sorting networks\nit lists.\n\n* Some of the optimizations used by `sorting_network_sorter` come from [this\ndiscussion](https://stackoverflow.com/q/2786899/1364752) on StackOverflow and are\nbacked by the article [*Applying Sorting Networks to Synthesize Optimized Sorting\nLibraries*](https://arxiv.org/abs/1505.01962).\n\n* The test suite reimplements random number algorithms originally found in the following places:\n  - [xoshiro256\\*\\*](https://prng.di.unimi.it/)\n  - [*Optimal Discrete Uniform Generation from Coin Flips, and Applications*](https://arxiv.org/abs/1304.1916)\n  - [*All numbers in a given range but random order*](https://stackoverflow.com/a/44821946/1364752)\n\n* The LaTeX scripts used to draw the sorting networks are modified versions of\nkaayy's [`sortingnetwork.tex`](https://github.com/kaayy/kaayy-s-code-sinppets),\nslightly adapted to be 0-based and draw the network from top to bottom.\n\n* The CMake tools embedded in the projects include scripts from [RWTH-HPC/CMake-codecov](https://github.com/RWTH-HPC/CMake-codecov)\nand [Crascit/DownloadProject](https://github.com/Crascit/DownloadProject).\n\n* Some of the benchmarks use a [colorblind-friendly palette](https://gist.github.com/thriveth/8560036)\ndeveloped by Thøger Rivera-Thorsen.\n\n\n  [adaptive-sort]: https://en.wikipedia.org/wiki/Adaptive_sort\n  [benchmarks]: https://github.com/Morwenn/cpp-sort/wiki/Benchmarks\n  [changelog]: https://github.com/Morwenn/cpp-sort/wiki/Changelog\n  [drop-merge-adapter]: https://github.com/Morwenn/cpp-sort/wiki/Sorter-adapters#drop_merge_adapter\n  [heap-sorter]: https://github.com/Morwenn/cpp-sort/wiki/Sorters#heap_sorter\n  [split-adapter]: https://github.com/Morwenn/cpp-sort/wiki/Sorter-adapters#split_adapter\n  [tooling]: https://github.com/Morwenn/cpp-sort/wiki/Tooling\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorwenn%2Fcpp-sort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorwenn%2Fcpp-sort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorwenn%2Fcpp-sort/lists"}