{"id":13730602,"url":"https://github.com/swenson/sort","last_synced_at":"2025-04-04T15:11:56.404Z","repository":{"id":1150183,"uuid":"1033853","full_name":"swenson/sort","owner":"swenson","description":"Sorting routine implementations in \"template\" C","archived":false,"fork":false,"pushed_at":"2024-02-13T04:28:02.000Z","size":407,"stargazers_count":466,"open_issues_count":5,"forks_count":89,"subscribers_count":25,"default_branch":"main","last_synced_at":"2025-03-28T14:11:24.306Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.caswenson.com","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/swenson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","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":"2010-10-29T02:41:28.000Z","updated_at":"2025-03-06T04:49:59.000Z","dependencies_parsed_at":"2024-12-13T19:12:22.680Z","dependency_job_id":"8af9b449-6fea-40ed-8104-a86ea52fa6fd","html_url":"https://github.com/swenson/sort","commit_stats":{"total_commits":146,"total_committers":19,"mean_commits":7.684210526315789,"dds":0.452054794520548,"last_synced_commit":"5820a8094e4a2ae1c88ac8f8df7735c332ee62ff"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swenson%2Fsort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swenson%2Fsort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swenson%2Fsort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swenson%2Fsort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swenson","download_url":"https://codeload.github.com/swenson/sort/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198469,"owners_count":20900081,"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":[],"created_at":"2024-08-03T02:01:17.066Z","updated_at":"2025-04-04T15:11:56.380Z","avatar_url":"https://github.com/swenson.png","language":"C","readme":"sort.h\n======\n\nOverview\n--------\n\n`sort.h` is an implementation of a ton of sorting algorithms in C with a\nuser-defined type that is provided at include time.\n\nThis means you don't have to pay the function call overhead of using\na standard library routine. This also gives us the power of higher-level\nlanguage generics.\n\nIn addition, you don't have to link in a library:\nthe entirety of this sorting library is contained in the `sort.h` header file.\n\nYou get the choice of many sorting routines, including:\n\n* Timsort (stable)\n* Quicksort\n* Merge sort (stable)\n* In-place merge sort (*not* stable)\n* Shellsort\n* Binary insertion sort\n* Heapsort\n\nIf you set `SORT_EXTRA` and have `sort_extra.h` available in the path, there are some additional, specialized sorting routines available:\n\n* Selection sort (this is really only here for comparison)\n* Bubble sort\n* Grail sort (stable)\n  * Based on [`B-C. Huang and M. A. Langston, *Fast Stable Merging and Sorting in\n  Constant Extra Space* (1989-1992)`](http://comjnl.oxfordjournals.org/content/35/6/643.full.pdf).\n\n    Thanks to Andrey Astrelin for the implementation.\n* Sqrt Sort (stable, based on Grail sort, also by Andrey Astrelin).\n\nIf you don't know which one to use, you should probably use Timsort.\n\nIf you have a lot data that is semi-structured, then you should definitely use Timsort.\n\nIf you have data that is really and truly random, quicksort is probably fastest.\n\n\nUsage\n-----\n\nTo use this library, you need to do three things:\n\n* `#define SORT_TYPE` to be the type of the elements of the array you\n  want to sort. (For pointers, you should declare this like: `#define SORT_TYPE int*`)\n* `#define SORT_NAME` to be a unique name that will be prepended to all\n  the routines, i.e., `#define SORT_NAME mine` would give you routines\n  named `mine_heap_sort`, and so forth.\n* `#include \"sort.h\"`.  Make sure that `sort.h` is in your include path.\n\nThen, enjoy using the sorting routines.\n\nQuick example:\n\n```c\n#define SORT_NAME int64\n#define SORT_TYPE int64_t\n#define SORT_CMP(x, y) ((x) - (y))\n#include \"sort.h\"\n```\n\nYou would now have access to `int64_quick_sort`, `int64_tim_sort`, etc.,\nwhich you can use like\n\n```c\n/* Assumes you have some int64_t *arr or int64_t arr[128]; */\nint64_quick_sort(arr, 128);\n```\n\nSee `demo.c` for a more detailed example usage.\n\nIf you are going to use your own custom type, you must redefine\n`SORT_CMP(x, y)` with your comparison function, so that it returns\na value less than zero if `x \u003c y`, equal to zero if `x == y`, and\ngreater than 0 if `x \u003e y`.\n\nThe default just uses the builtin `\u003c` operators:\n\n```c\n#define SORT_CMP(x, y)  ((x) \u003c (y) ? -1 : ((y) \u003c (x) ? 1 : 0))\n```\n\nIt is often just fine to just subtract the arguments as well (though\nthis can cause some stability problems with floating-point types):\n\n```c\n#define SORT_CMP(x, y) ((x) - (y))\n```\n\nYou can also redefine `TIM_SORT_STACK_SIZE` (default 128) to control\nthe size of the tim sort stack (which can be used to reduce memory).\nReducing it too far can cause tim sort to overflow the stack though.\n\nYou can specify definitions for all functions that are included in\nsort.h.  Making sort functions static increases the likelihood a\ncompiler will eliminate dead code.\n\n```c\n#define SORT_DEF static\n```\n\nSpeed of routines\n-----------------\n\nThe speed of each routine is highly dependent on your computer and the\nstructure of your data.\n\nIf your data has a lot of partially sorted sequences, then Tim sort\nwill beat the kilt off of anything else.\n\nTimsort is not as good if memory movement is many orders of magnitude more\nexpensive than comparisons (like, many more than for normal int and double).\nIf so, then quick sort is probably your routine.  On the other hand, Timsort\ndoes extremely well if the comparison operator is very expensive,\nsince it strives hard to minimize comparisons.\n\nHere is the output of `demo.c`, which will give you the timings for a run of\n10,000 `int64_t`s on 2014-era MacBook Pro:\n\n```\nRunning tests\nstdlib qsort time:             1285.00 us per iteration\nstdlib heapsort time:          2109.00 us per iteration\nstdlib mergesort time:         1299.00 us per iteration\nquick sort time:                579.00 us per iteration\nselection sort time:         127176.00 us per iteration\nmerge sort time:                999.00 us per iteration\nbinary insertion sort time:   13443.00 us per iteration\nheap sort time:                 592.00 us per iteration\nshell sort time:               1054.00 us per iteration\ntim sort time:                 1005.00 us per iteration\nin-place merge sort time:       903.00 us per iteration\ngrail sort time:               1220.00 us per iteration\nsqrt sort time:                1095.00 us per iteration\n```\n\nQuicksort is the winner here. Heapsort, in-place merge sort,\nand timsort also often tend to be quite fast.\n\nContributing\n------------\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md).\n\nReferences\n----------\n\n* [Wikipedia: Timsort](https://en.wikipedia.org/wiki/Timsort)\n* [`timsort.md`](doc/timsort.txt)\n\nLicense\n-------\n\nAvailable under the MIT License. See [LICENSE.md](LICENSE.md) for details.\n","funding_links":[],"categories":["Utilities","公用事业","C++"],"sub_categories":["YAML"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswenson%2Fsort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswenson%2Fsort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswenson%2Fsort/lists"}