@shlappas/sorted-array
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

Sorted Array

Sometimes you have an array that needs to be kept in order through pushes and pops etc.

// Becomes slow as arrays get larger. O(n log n)
arr.push(value)
arr.sort()

// May check all values; not binary search.
arr.indexOf(value)

This module provides an API similar to the builtin javascript Array, but remains sorted after insertion and deletion.

const arr = new SortedArray((a, b) => a - b)

// Each insert runs in O(log n)
arr.insert(5) // [ 5 ]
arr.insert(10) // [ 5 10 ]
arr.insert(1) // [ 1 5 10 ]
arr.insert(5) // [ 1 5 5 10 ]

arr.forEach((v) => console.log(v)) // 1 5 5 10

Usage

Check out the docs!

Installation

yarn add @shlappas/sorted-array

See Also

This modules is very similar to sorted-set. (both use a weight-balanced tree in the background)

Package Sidebar

Install

npm i @shlappas/sorted-array

Weekly Downloads

39

Version

1.2.0

License

MIT

Unpacked Size

41.9 kB

Total Files

8

Last publish

Collaborators

  • shlappas