{"id":15706570,"url":"https://github.com/general-cbic/ruby-heap","last_synced_at":"2025-05-12T15:21:37.078Z","repository":{"id":37045886,"uuid":"93414678","full_name":"general-CbIC/ruby-heap","owner":"general-CbIC","description":"Binary or multiple heap (heapsort)","archived":false,"fork":false,"pushed_at":"2025-03-13T04:48:59.000Z","size":330,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-04-16T05:18:23.776Z","etag":null,"topics":["heapsort","ruby","sort","sorting-algorithms-implemented"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/general-CbIC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":"general-CbIC"}},"created_at":"2017-06-05T14:50:16.000Z","updated_at":"2025-03-13T04:49:01.000Z","dependencies_parsed_at":"2024-06-20T16:52:15.536Z","dependency_job_id":"201386ce-3114-468b-b075-ca229c6fb2d1","html_url":"https://github.com/general-CbIC/ruby-heap","commit_stats":null,"previous_names":["pups3s/ruby-heap"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/general-CbIC%2Fruby-heap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/general-CbIC%2Fruby-heap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/general-CbIC%2Fruby-heap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/general-CbIC%2Fruby-heap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/general-CbIC","download_url":"https://codeload.github.com/general-CbIC/ruby-heap/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249902386,"owners_count":21342827,"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":["heapsort","ruby","sort","sorting-algorithms-implemented"],"created_at":"2024-10-03T20:24:27.474Z","updated_at":"2025-04-20T13:31:11.032Z","avatar_url":"https://github.com/general-CbIC.png","language":"Ruby","funding_links":["https://github.com/sponsors/general-CbIC"],"categories":[],"sub_categories":[],"readme":"# Heap (ruby heapsort)\n\n[![Gem Version](https://badge.fury.io/rb/ruby-heap.svg)](https://badge.fury.io/rb/ruby-heap)\n![Build workflow](https://github.com/general-CbIC/ruby-heap/actions/workflows/build.yml/badge.svg)\n[![Gem](https://img.shields.io/gem/dt/ruby-heap.svg)](https://rubygems.org/gems/ruby-heap)\n[![Code Climate](https://codeclimate.com/github/pups3s/ruby-heap/badges/gpa.svg)](https://codeclimate.com/github/pups3s/ruby-heap)\n\n[Русская версия README](README_ru.md)\n\nGem is used to create Heap structs and sort via [Heapsort algorithm](https://en.wikipedia.org/wiki/Heapsort).\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'ruby-heap'\n```\n\nAnd then execute:\n\n```bash\nbundle\n```\n\nOr install it yourself as:\n\n```bash\ngem install ruby-heap\n```\n\n## Usage\n\n### Binary Heaps\n\n#### Binary Heap with min root\n\nWhile Heap initialize you can add any comparable object in it (not numbers only).\nObjects must have compare functions (\u003e, \u003e=, \u003c, \u003c=).\n\n```ruby\nrequire 'Heap'\n\n# Initialize\nb_heap = Heap::BinaryHeap::MinHeap.new([2, 3, 1, -1])\n\n# Return elements in Heap (read access only)\nb_heap.elements     # [-1, 1, 3, 2]\n\n# Return sorted array (heap-sort) without\n# changing elements in heap\nb_heap.sort         # [-1, 1, 2, 3]\n\n# Count of elements in Heap\nb_heap.count        # 4\n\n# Return min element without removing from Heap\nb_heap.extract_min  # -1\n\n# Return min element and remove it from Heap\nb_heap.extract_min! # -1\nb_heap.count        # 3\nb_heap.elements     # [1, 2, 3]\n\n# Also you can add elements with \"add\" function\nb_heap.add -1\nb_heap.elements     # [-1, 1, 3, 2]\nb_heap.add [0, 9, 200, -15, 6]\nb_heap.elements     # [-15, -1, 3, 0, 1, 9, 200, 2, 6]\nb_heap.sort         # [-15, -1, 0, 1, 2, 3, 6, 9, 200]\n```\n\n#### Same practice with max root\n\n```ruby\nrequire 'Heap'\n\n# Initialize\nb_heap = Heap::BinaryHeap::MaxHeap.new([2, 3, 1, -1])\n\n# Return elements in Heap (read access only)\nb_heap.elements     # [3, 2, 1, -1]\n\n# Return sorted array (heap-sort) without\n# changing elements in heap\nb_heap.sort         # [3, 2, 1, -1]\n\n# Count of elements in Heap\nb_heap.count        # 4\n\n# Return max element without removing from Heap\nb_heap.extract_max  # 3\n\n# Return max element and remove it from Heap\nb_heap.extract_max! # 3\nb_heap.count        # 3\nb_heap.elements     # [2, -1, 1]\n\n# Also you can add elements with \"add\" function\nb_heap.add -1\nb_heap.elements     # [2, -1, 1, -1]\nb_heap.add [0, 9, 200, -15, 6]\nb_heap.elements     # [200, 6, 9, 0, -1, 1, 2, -15, -1]\nb_heap.sort         # [200, 9, 6, 2, 1, 0, -1, -1, -15]\n```\n\n#### Heap merging\n\n```ruby\nrequire 'Heap'\n\n# Initialize heaps\nmin_heap = Heap::BinaryHeap::MinHeap.new [1, 2, 3]\nmax_heap = Heap::BinaryHeap::MaxHeap.new [9, -1, 4]\n\n# Merge heaps\nmin_heap.add max_heap\n\nmin_heap.count    # 6\nmin_heap.sort     # [-1, 1, 2, 3, 4, 9]\n```\n\n### Multiple Heaps\n\nMultiple (d-ary) heaps have **same methods as binary**. But initialize differs:\n\n```ruby\nrequire 'Heap'\n\n# First param is \"d\" of heap\n# Second param is optional and can contain first elements\nmin_heap = Heap::MultipleHeap::MinHeap.new(5, [10, 20, 30])\nmax_heap = Heap::MultipleHeap::MaxHeap.new(7)\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at [Project page](https://github.com/pups3s/ruby-heap).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeneral-cbic%2Fruby-heap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeneral-cbic%2Fruby-heap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeneral-cbic%2Fruby-heap/lists"}