{"id":24830248,"url":"https://github.com/boristane/arr-sorting","last_synced_at":"2025-10-10T23:34:03.590Z","repository":{"id":57183849,"uuid":"158135585","full_name":"boristane/arr-sorting","owner":"boristane","description":"Sort an array using various popular sorting algorithms.","archived":false,"fork":false,"pushed_at":"2019-08-01T07:05:46.000Z","size":1160,"stargazers_count":2,"open_issues_count":15,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-30T09:21:32.862Z","etag":null,"topics":["algorithm","javascript","sorting","sorting-algorithms"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/boristane.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-18T23:04:14.000Z","updated_at":"2019-01-30T22:05:10.000Z","dependencies_parsed_at":"2022-08-23T01:20:41.358Z","dependency_job_id":null,"html_url":"https://github.com/boristane/arr-sorting","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boristane%2Farr-sorting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boristane%2Farr-sorting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boristane%2Farr-sorting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boristane%2Farr-sorting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boristane","download_url":"https://codeload.github.com/boristane/arr-sorting/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245579576,"owners_count":20638676,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","javascript","sorting","sorting-algorithms"],"created_at":"2025-01-30T23:49:28.956Z","updated_at":"2025-10-10T23:33:58.530Z","avatar_url":"https://github.com/boristane.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# arr-sorting\r\n\r\nSort an array using various popular sorting algorithms in javascript.\r\n\r\n[![Build Status](https://travis-ci.org/boristane/arr-sorting.svg?branch=master)](https://travis-ci.org/boristane/arr-sorting) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/6f4ef9df6de64d15b32d824c89b05b5e)](https://www.codacy.com/app/boris.tane/arr-sorting?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=boristane/arr-sorting\u0026utm_campaign=Badge_Grade) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/6f4ef9df6de64d15b32d824c89b05b5e)](https://www.codacy.com/app/boris.tane/arr-sorting?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=boristane/arr-sorting\u0026utm_campaign=Badge_Coverage) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)\r\n\r\n[![NPM](https://nodei.co/npm/arr-sorting.png?compact=true)](https://nodei.co/npm/arr-sorting/)\r\n\r\n## Installation\r\n\r\nInstall via `npm`\r\n\r\n```bash\r\nnpm install arr-sorting\r\n```\r\n\r\n## Getting Started\r\n\r\nThe library contains a set of functions performing array sorting using various sorting algorithms.\r\n\r\nThe sorting functions are not necessarily stable in the algorithmic sense.\r\n\r\nAll functions work as per the following schema:\r\n\r\n```js\r\nconst arr = [2, 5, 10, 5, 32, 6];\r\nconst arrObjects = [ { a: 5, }, { a: 1, }, { a: 4, }, { a: 8, }, ];\r\nalgo(arr); // [2, 5, 5, 6, 10, 32]\r\nalgo(arr, (a, b) =\u003e b - a); // [32, 10, 6, 5, 5, 2]\r\nalgo(arrObjects, (obj1, obj2) =\u003e obj1.a - obj2.a); // [ { a: 1, }, { a: 4, }, { a: 5, }, { a: 8, } ]\r\n```\r\n\r\nwhere `algo` is one of the following functions:\r\n\r\n-   [bubble](#bubble)\r\n-   [insertion](#insertion)\r\n-   [merge](#merge)\r\n-   [shell](#shell)\r\n-   [heap](#heap)\r\n-   [selection](#selection)\r\n-   [binary](#binary)\r\n-   [tim](#tim)\r\n\r\n## Documentation\r\n\r\n### bubble\r\n\r\nSorts an array according to a compare function using the [bubble sort algorithm](https://en.wikipedia.org/wiki/Bubble_sort).\r\n\r\nThe bubble sort algorithm repeatedly steps through the aray, compares adjacent pairs and swaps them if they are in the wrong order. The algorithm goes through the array until it is sorted.\r\n\r\nBubble sort is one of the simpler sorting algorithm and proves to be too slow when compared to [insertion sort](#insertion). As such, it is impractical in most use cases, with the exception of cases where the array is mostly sorted, with some out-of-order elements nearly in position.\r\n\r\n| Case       | Complexity    |\r\n| ---------- |:-------------:|\r\n| Best       | O(n)          |\r\n| Average    | O(n^2)        |\r\n| Worst      | O(n^2)        |\r\n\r\n\r\n### insertion\r\n\r\nSorts an array according to a compare function using the [insertion sort algorithm](https://en.wikipedia.org/wiki/Insertion_sort).\r\n\r\nThe insertion sort algorithm iterates through the array, considering one element each repetition, compares it iteratively to the elements of the array with an index lower until it finds its location and insets it there. This is repeated until the last element of the array.\r\n\r\n| Case       | Complexity    |\r\n| ---------- |:-------------:|\r\n| Best       | O(n)          |\r\n| Average    | O(n^2)        |\r\n| Worst      | O(n^2)        |\r\n\r\n### merge\r\n\r\nSorts an array according to a compare function using the [merge sort algorithm](https://en.wikipedia.org/wiki/Merge_sort).\r\n\r\nThe merge sort algorithm works in two steps:\r\n\r\n* Divide the unsorted array into sub-arrays, each containing one element (the number of sub-arrays is the lenght of the array).\r\n* Repeatedly merge sub-arrays to produce new sorted sub-arrays until there is only one sub-array remaining. This will be the sorted array.\r\n\r\nThe merge sort algorithm is generally more efficient than the simpler [bubble](#bubble) and [insertion](#insertion) algorithms.\r\n\r\n| Case       | Complexity    |\r\n| ---------- |:-------------:|\r\n| Best       | O(n log(n))   |\r\n| Average    | O(n log(n))   |\r\n| Worst      | O(n log(n))   |\r\n\r\n### shell\r\n\r\nSorts an array according to a compare function using the [shell sort algorithm](https://en.wikipedia.org/wiki/Shellsort).\r\n\r\nThe shell sort algorithm is a variation of [insertion](#insertion) sort. Where in insertion sort, elements of the array are compared to elements one index lower, shell sort allows to compare elements that are far apart. In shell sort, a gap `h` is defined and the elements of the array are compared to elements `h` index lower. The gap `h` is reduced until it becomes 1. An array is said to be h-sorted if all sub-array of every `h`’th element is sorted.\r\n\r\n| Case       | Complexity      |\r\n| ---------- |:---------------:|\r\n| Best       | O(n log(n))     |\r\n| Average    | O(n (log(n))^2) |\r\n| Worst      | O(n (log(n))^2) |\r\n\r\n### heap\r\n\r\nSorts an array according to a compare function using the [heap sort algorithm](https://en.wikipedia.org/wiki/Heapsort).\r\n\r\nThe heap sort algorithm is based on the [heap data structure](https://en.wikipedia.org/wiki/Heap_(data_structure)). A heap is a tree-based data structure the following \"heap\" property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C.\r\n\r\nThe node at the \"top\" of the heap (with no parents) is called the root node.\r\n\r\nThe heapsort algorithm can be devided into two steps:\r\n\r\n* Prepare the array by turning it into a max heap. \r\n* Repeatedly swap the first element of the array with its last one, and updating the heap such that its \"heap\" property is preserved.\r\n\r\n\r\n| Case       | Complexity      |\r\n| ---------- |:---------------:|\r\n| Best       | O(n log(n))     |\r\n| Average    | O(n log(n))     |\r\n| Worst      | O(n log(n))     |\r\n\r\n### selection\r\n\r\nSorts an array according to a compare function using the [selection sort algorithm](https://en.wikipedia.org/wiki/Selection_sort).\r\n\r\nThe selection sort algorithm divides the input array into two parts: \r\n* The left sub-array which is sorted.\r\n* The right sub-array with the unsorted elements.\r\n\r\nThe algorithm iteratively goes through the unsorted sub-array and searches for its smallest element, which is added at the end of the sorted array.\r\n\r\n| Case       | Complexity      |\r\n| ---------- |:---------------:|\r\n| Best       | O(n^2)          |\r\n| Average    | O(n^2)          |\r\n| Worst      | O(n^2)          |\r\n\r\n### binary\r\n\r\nSorts an array according to a compare function using the [binary sort algorithm](https://en.wikipedia.org/wiki/Tree_sort).\r\n\r\nThe binary sort algorithm \r\n\r\n### quick\r\n\r\nSorts an array according to a compare function using the [quick sort algorithm](https://en.wikipedia.org/wiki/Quicksort).\r\n\r\nThe quick sort algorith, divides an array into two sub-arrays, and recursively sorts the sub-arrays. It follows three steps:\r\n* Select an element (the pivot) from the array.\r\n* Partition (re-arrange) the array such that all elements with values comparatively smaller than the pivot come before the pivot.\r\n* Recursively repeat the above two steps to the sub-array of elements comparatively smaller than the pivot.\r\n\r\nThis implementation of quick sort selects the pivot as the last element of the array.\r\n\r\n| Case       | Complexity      |\r\n| ---------- |:---------------:|\r\n| Best       | O(n log(n))     |\r\n| Average    | O(n log(n))     |\r\n| Worst      | O(n^2)          |\r\n\r\n### tim\r\n\r\nSorts an array according to a compare function using the [timsort algorithm](https://en.wikipedia.org/wiki/Timsort).\r\n\r\nThe timsort algorithm is an hybrid soring algorithm based on [insertion sort](#insertion) and [merge sort](#merge). The array is divided into sub-arrays (runs). The runs are then sorted using [insertion sort](#insertion). The sorted runs are afterwards merged using the merged function in [merge sort](#merge).\r\n\r\nThis implementation of timsort has a fixed run length of 32, to reduce complexity and improve performace ([the merge function performs better on sub-array with size as powers of 2](https://www.geeksforgeeks.org/timsort/)).\r\n\r\n| Case       | Complexity      |\r\n| ---------- |:---------------:|\r\n| Best       | O(n)            |\r\n| Average    | O(n log(n))     |\r\n| Worst      | O(n log(n))     |\r\n\r\n\r\n## License\r\n\r\nThe library is [MIT licensed](LICENSE).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboristane%2Farr-sorting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboristane%2Farr-sorting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboristane%2Farr-sorting/lists"}