Module: Mergesort

Mergesort module

Source:

(require("Mergesort"))(config) → {module:Mergesort~instance}

Returns a Mergesort instance


let instance = Mergesort(); //switches to insertion sort for array fragments < 16

instance = Mergesort({threshold: 64}); //will switch to insertion sort for fragments < 64

instance = Mergesort({size:999}); //will throw an error if you try to sort arrays with length other than 999

Parameters:
Name Type Description
config module:Mergesort~config

a configuration object with optional parameters (default 16)

Source:
Returns:
Type
module:Mergesort~instance

Namespaces

instance

Type Definitions

config

An optinal configuration object that can be passed to the Mergesort factory function

Type:
  • Object
Properties:
Name Type Description
threshold number

The threshold below where the algorithm temporarily switches over to insertion sort

size number

Use this if you are going to consistently sort arrays of fixed size. Refers to size of the array to be sorted, where a binary tree is precalculated. The tree will be reused for each call of the returned instance. For each use, the tree is walked by setting firstChild properties of leaf nodes to null and then regenerated by setting them again from the lastChild property of their parent. This option results in a performance gain for large (> 1M) arrays, where cost of creating the tree is greater than the cost of walking + regenerating the tree. For small arrays, it has the reverse effect. It is set to off by default.

Source: