New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

heapq

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

heapq

Heap queue implementation for nodejs/iojs.

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

heapq.js

Heap queue implementation for nodejs/iojs.

var heapq = require('heapq');

var heap = [];

heapq.push(heap, [3, 'a']);
heapq.push(heap, [4, 'b']);
heapq.push(heap, [2, 'c']);
heapq.push(heap, [1, 'd']);

heapq.pop(heap);  // [1, 'd']
heapq.pop(heap);  // [2, 'c']

API Refs

  • heapq.push(heap, item), push an item into heap. O(log n)
  • heapq.pop(heap), pop the smallest item from heap. O(log n)
  • heapq.top(heap), get the smallest item. O(1)
  • heapq.pushpop(heap), push an item on the heap and pop out the smallest item, this runs more efficiently than heapq.push() followed by a separate call to heapq.pop(). O(log n)
  • heapq.heapify(arr), transform array heap into a heap in-place. O(nlog n)

Heap Sort

An example of heap sort: O(nlog n):

var heapq = require('heapq');

function heapSort(arr) {
  var heap = [];

  for (var i = 0; i < arr.length; ++i)
    heapq.push(heap, arr[i]);

  var arr_ = [];

  while (heap.length > 0)
    arr_.push(heapq.pop(heap));

  return arr_;
}

heapSort([3, 4, 5, 0, 2, 1]); // [0, 1, 2, 3, 4, 5]

License

MIT (c) Chao Wang hit9@icloud.com

Keywords

FAQs

Package last updated on 20 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc