merge-sort-nat
Version:
Merge sort library made for testing class
101 lines (61 loc) • 1.8 kB
Markdown
# Merge sort library
## [Merge sort algorithm ](https://en.wikipedia.org/wiki/Merge_sort) implementation in node.js and typescript
## To install run following command in your terminal:
### Npm
```shell
npm install merge-sort-nat
```
### Yarn
```shell
yarn add merge-sort-nat
```
> **_Currently works only in node environment_**
## Get starting example:
```js
const { mergeSort } = require("merge-sort-nat"); // Import function
let array = [5, 4, 3, 2, 1]; // Create array
let sortedArray = mergeSort(array, (a, b) => a - b); // Sort array in ascending order
console.log(sortedArray); // Display result in console
```
## Documentation:
### `mergeSort`
Function that takes array and returns new sorted array
#### **_Parameters_**:
##### `array:T[]`
Array that we need to sort
##### `compareFunction:(firstElement: T,secondElement: T) => number`
Function that takes two elements and changes their order basing on output of function:
If output number is **below** `0`, it takes `firstElement` <br>
If output number is **above** `0`, it takes `secondElement` <br>
If output number is **equal** `0`, it does not change order of elements
## Tests
#### Tests implemented in [jest](https://jestjs.io) and located in [\_\_tests\_\_](https://github.com/amazzat/merge-sort/tree/master/src/__tests__) folder
#### To run tests locally:
- Clone repository
```shell
git clone https://github.com/amazzat/merge-sort.git zulpykhar_azamat_cs2005_merge_sort
```
- Enter the folder
```shell
cd ./zulpykhar_azamat_cs2005_merge_sort
```
- Install packages
```shell
npm install
```
or
```shell
yarn
```
- Run test command
```shell
npm test
```
or
```shell
yarn test
```
#### **License**: MIT
#### **Author**: Zulpykhar Azamat
#### **Group**: CS2005
#### Astana IT University 2020-2023