@strong-roots-capital/merge-sorted
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

merge-sorted Build status npm version codecov

Merge two sorted arrays in O(n) time and O(n) space

Install

npm install @strong-roots-capital/merge-sorted

Use

import mergeSorted from '@strong-roots-capital/merge-sorted'

function compareNumber(a: number, b: number): number {
    return a - b
}

test('should merge two sorted arrays of same length', t => {

    const a = [1, 3, 5]
    const b = [2, 4, 6]

    t.deepEqual(
        [1, 2, 3, 4, 5, 6],
        mergeSorted(a, b, compareNumber)
    )
})

test('should merge two sorted arrays of different lengths', t => {

    const a = [1, 2, 3, 5, 6]
    const b = [4]

    t.deepEqual(
        [1, 2, 3, 4, 5, 6],
        mergeSorted(a, b, compareNumber)
    )
})

test('should return empty array when both inputs are empty', t => {
    t.deepEqual([], mergeSorted([], [], compareNumber))
})

Contributors

Acknowledgments

Index

Functions

Functions

mergeSorted

mergeSortedT›(a: T[], b: T[], comparator: function): T[]

Defined in merge-sorted.ts:12

Type parameters:

T

Parameters:

a: T[]

b: T[]

comparator: function

▸ (a: T, b: T): number

Parameters:

Name Type
a T
b T

Returns: T[]

Sorted array containing all elements of a and b

Readme

Keywords

Package Sidebar

Install

npm i @strong-roots-capital/merge-sorted

Weekly Downloads

16

Version

1.0.2

License

ISC

Unpacked Size

7.75 kB

Total Files

6

Last publish

Collaborators

  • amchelle
  • hamroctopus