{"id":23193583,"url":"https://github.com/mcraiha/csharp-nth_element","last_synced_at":"2026-02-22T10:36:16.074Z","repository":{"id":34903409,"uuid":"38939563","full_name":"mcraiha/CSharp-nth_element","owner":"mcraiha","description":"CSharp (C#) versions of std::nth_element (or almost)","archived":false,"fork":false,"pushed_at":"2022-12-03T19:25:46.000Z","size":30,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-23T03:48:41.683Z","etag":null,"topics":["csharp","nthelement"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mcraiha.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-11T20:32:19.000Z","updated_at":"2022-11-29T16:16:29.000Z","dependencies_parsed_at":"2023-01-15T10:15:30.628Z","dependency_job_id":null,"html_url":"https://github.com/mcraiha/CSharp-nth_element","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mcraiha/CSharp-nth_element","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcraiha%2FCSharp-nth_element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcraiha%2FCSharp-nth_element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcraiha%2FCSharp-nth_element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcraiha%2FCSharp-nth_element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcraiha","download_url":"https://codeload.github.com/mcraiha/CSharp-nth_element/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcraiha%2FCSharp-nth_element/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29709413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-22T10:34:24.778Z","status":"ssl_error","status_checked_at":"2026-02-22T10:32:23.200Z","response_time":110,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["csharp","nthelement"],"created_at":"2024-12-18T13:11:52.882Z","updated_at":"2026-02-22T10:36:16.055Z","avatar_url":"https://github.com/mcraiha.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSharp-nth_element\nCSharp (C#) versions of std::nth_element (or almost)\n\n## Status\n[![.NET](https://github.com/mcraiha/CSharp-nth_element/actions/workflows/dotnet-tests.yml/badge.svg)](https://github.com/mcraiha/CSharp-nth_element/actions/workflows/dotnet-tests.yml)\n\n## Introduction to this project\nSince C# doesn't have build-in replacement for std::nth_element, I created few versions from existing implementations. **WARNING** All error handling code has been stripped away, so YMMV\n\n## Introduction to std::nth_element\n[std::nth_element](http://www.cplusplus.com/reference/algorithm/nth_element/) does two things if you use it to **array** with index **K**:\n\n1. item in index K is same as it would be if array was sorted\n2. Items before index K are \u003c= or \u003e= and items after K are \u003e= or \u003c= based on your sorting method\n\ne.g. we have input array of ints (5 4 8 9 1 6 3 2 7) and we want to find out the 4th smallest number from that array. With std::nth_element using 4 as K, the output array would be (2 1 3 **4** 9 6 8 5 7). Fourth element is 4, and elements before it are smaller than 4, elements after it are larger than 4. \n\nSince there are multiple ways to implement std::nth_element, the outputs can vary. e.g. for our earlier example (5 4 8 9 1 6 3 2 7) with K as 4, another valid output would be (1 2 3 **4** 9 7 8 5 6). Output can also be fully sorted (1 2 3 **4** 5 6 7 8 9), but that is **NEVER** guaranteed for non trival sized arrays.\n\nThere two main uses for std::nth_element:\n* Find nth largest/smallest item from array (useful if you need e.g. find the median value from array)\n* Give helping hand to sorting algorithms by splitting their work\n\n## Differences between C++ and C# versions\nC++ version uses iterators. C# versions presented in here use zero based indexes (e.g. [0] means first item in array).\n\n## Implementations\nFirst version (**nthelement-PD.cs**) comes from **Adam Horvath**. His [blog](http://blog.teamleadnet.com/2012/07/quick-select-algorithm-find-kth-element.html) has an Java example which uses Quick select algorithm.\n\nSecond one (**nthelement-GPLv2.cs**) follows the [C source code](http://sourcecodebrowser.com/cdo/1.4.0.1~dfsg/nth__element_8c_source.html) given in CDO project.\n\n## Examples\nYou have following C++ line of code\n```c++\nstd::nth_element(myvector.begin(), myvector.begin() + 4, myvector.end());\n```\nyou would replace it with\n```cs\nPartialSort.nth_element(array: myArray, startIndex: 0, nthSmallest: 4, endIndex: myArray.Length - 1);\n```\n\nIf you want to use custom comparer (like descending order), follow this example\n```cs\nPartialSort.nth_element(array: arr, startIndex: 0, nthToSeek: 10, endIndex: arr.Length - 1, (i1, i2) =\u003e i2.CompareTo(i1));\n```\n\n## Benchmarks\n\nYou can run benchmarks (which compares both implementations) by moving to **benchmarks** folder and running following command\n```bash\ndotnet run -c Release\n```\n\nthere are three different input sizes (16 bytes, 64 bytes and 1024 bytes)\n\n## Licenses\nThis text file (**README.md**) is licensed under Creative Commons Zero (CC0 1.0 Universal), see [LICENSE](https://github.com/mcraiha/CSharp-nth_element/blob/master/LICENSE) file\n\n**nthelement-PD.cs** is released into the public domain. See [PUBLICDOMAIN](https://github.com/mcraiha/CSharp-nth_element/blob/master/PUBLICDOMAIN) file\n\n**nthelement-GPLv2.cs** is licensed under GPLv2, see [COPYING](https://github.com/mcraiha/CSharp-nth_element/blob/master/COPYING) file\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcraiha%2Fcsharp-nth_element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcraiha%2Fcsharp-nth_element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcraiha%2Fcsharp-nth_element/lists"}