{"id":21681844,"url":"https://github.com/alvarocastro/heapsort","last_synced_at":"2026-04-18T10:35:34.468Z","repository":{"id":40211931,"uuid":"205400838","full_name":"alvarocastro/heapsort","owner":"alvarocastro","description":"Yet another implementation of heapsort in JavaScript aimed to be flexible, lightweight and fast.","archived":false,"fork":false,"pushed_at":"2023-01-26T01:03:46.000Z","size":1414,"stargazers_count":0,"open_issues_count":13,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-03T00:38:06.821Z","etag":null,"topics":["algorithm","heapsort","sort"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@alvarocastro/heapsort","language":"JavaScript","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/alvarocastro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-30T14:42:07.000Z","updated_at":"2021-05-06T22:30:08.000Z","dependencies_parsed_at":"2023-02-14T12:55:16.047Z","dependency_job_id":null,"html_url":"https://github.com/alvarocastro/heapsort","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/alvarocastro/heapsort","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fheapsort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fheapsort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fheapsort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fheapsort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alvarocastro","download_url":"https://codeload.github.com/alvarocastro/heapsort/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fheapsort/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266828795,"owners_count":23991220,"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","status":"online","status_checked_at":"2025-07-24T02:00:09.469Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["algorithm","heapsort","sort"],"created_at":"2024-11-25T15:31:57.378Z","updated_at":"2025-10-17T18:13:55.164Z","avatar_url":"https://github.com/alvarocastro.png","language":"JavaScript","readme":"# Heapsort\n\n[![NPM](https://img.shields.io/npm/v/@alvarocastro/heapsort)](https://www.npmjs.com/package/@alvarocastro/heapsort)\n[![Build status](https://img.shields.io/github/workflow/status/alvarocastro/heapsort/build)](https://github.com/alvarocastro/heapsort/actions?query=workflow%3Abuild)\n[![Maintainability status](https://img.shields.io/codeclimate/maintainability/alvarocastro/heapsort)](https://codeclimate.com/github/alvarocastro/heapsort/maintainability)\n[![Coverage status](https://img.shields.io/coveralls/github/alvarocastro/heapsort)](https://coveralls.io/github/alvarocastro/heapsort?branch=master)\n[![Bundle size](https://img.shields.io/bundlephobia/min/@alvarocastro/heapsort)](https://bundlephobia.com/result?p=@alvarocastro/heapsort)\n[![Code style: XO](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)\n[![Release: Semantic](https://img.shields.io/badge/%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\nYet another implementation of heapsort in JavaScript aimed to be flexible, lightweight and fast.\n\n- [Install](#install)\n- [Usage](#usage)\n- [Performance](#performance)\n- [More examples](#more-examples)\n- [Contributing](#contributing)\n- [Support](#support)\n\n## Install\n\n```bash\nnpm install @alvarocastro/heapsort\n```\n\n## Usage\n\n```js\nconst sort = require('@alvarocastro/heapsort');\n\nconst elements = [8, -1, 3, 0.5, 200];\nsort(elements);\n// =\u003e [-1, 0.5, 3, 8, 200]\n```\n\n### sort(elements[, compare])\n\nReturns a new sorted array based on the `compare` function criteria.\n\n#### elements\n\nType: `Array`\n\nList of elements to sort.\n\n#### compare\n\nType: `Function`\u003cbr\u003e\nDefault: [comparatorAscending](utils.js#L2)\n\nThe function to use to compare two elements and find their sorting order.\nThe expected return of the function is:\n* `-1` to sort the element to the left.\n* `1` to sort the element to the right.\n* `0` when the elements are the same, no sorting is made.\n\nA descending function is also provided in [utils.js](utils.js).\n\n## Performance\n\nWant to test the performance?\n\nIncluded is the command `npm run performance` that will run a battery of profiled tests for your needs.\n\nHere is an example of the output (YMMV depending your hardware)\n```\nSorting random numbers generated in the range [-1,1]:\n#1 - 10 numbers\n\u003e Heapsort: 0ms\n\u003e Array.sort: 1ms\n#2 - 100 numbers\n\u003e Heapsort: 2ms\n\u003e Array.sort: 1ms\n#3 - 1000 numbers\n\u003e Heapsort: 8ms\n\u003e Array.sort: 1ms\n#4 - 10000 numbers\n\u003e Heapsort: 16ms\n\u003e Array.sort: 23ms\n#5 - 100000 numbers\n\u003e Heapsort: 45ms\n\u003e Array.sort: 394ms\n#6 - 1000000 numbers\n\u003e Heapsort: 709ms\n\u003e Array.sort: 6260ms\n```\n\n## More examples\n\n### Reverse order\n\n```js\nconst sort = require('@alvarocastro/heapsort');\nconst {comparatorDescending} = require('@alvarocastro/heapsort/utils');\n\nconst elements = [8, -1, 3, 0.5, 200];\nsort(elements, comparatorDescending);\n// =\u003e [200, 8, 3, 0.5, -1]\n```\n\n### Custom elements\n\n```js\nconst sort = require('@alvarocastro/heapsort');\n\nconst elements = [\n\t{name: 'Sarah Connor', firstAppearance: 'The Terminator'},\n\t{name: 'T-800', firstAppearance: 'The Terminator'},\n\t{name: 'Kyle Reese', firstAppearance: 'The Terminator'},\n\t{name: 'John Connor', firstAppearance: 'Terminator 2: Judgement Day'},\n\t{name: 'T-1000', firstAppearance: 'Terminator 2: Judgement Day'}\n];\nconst comparator = function (a, b) {\n\tif (a.name \u003c b.name) {\n\t\treturn -1;\n\t} else if (a.name \u003e b.name) {\n\t\treturn 1;\n\t}\n\treturn 0;\n};\nsort(elements, comparator);\n// =\u003e [\n// {name: 'John Connor', firstAppearance: 'Terminator 2: Judgement Day'},\n// {name: 'Kyle Reese', firstAppearance: 'The Terminator'},\n// {name: 'Sarah Connor', firstAppearance: 'The Terminator'},\n// {name: 'T-800', firstAppearance: 'The Terminator'},\n// {name: 'T-1000', firstAppearance: 'Terminator 2: Judgement Day'}\n// ]\n```\n\n## Contributing\n\nContributions are always welcome! Please run `npm test` beforehand to ensure everything is ok.\n\n## Support\n\nIf you use this package please consider starring it :)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvarocastro%2Fheapsort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falvarocastro%2Fheapsort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvarocastro%2Fheapsort/lists"}