-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascriptSort.js
More file actions
31 lines (29 loc) · 865 Bytes
/
javascriptSort.js
File metadata and controls
31 lines (29 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// @ts-check
"use strict";
/**
* Javascript Sort Non Mutating
* Non Mutating. Sorts an array using the built in Javascript Sort algorithm, String-Based Lexicographic Sort with Timsort
* @param {Array} myArray - The input array to be sorted.
* @return {Array} - The sorted array.
* @complexity O(n²)
* @author Joshua Jarman
*/
export function javascriptSortNonMutating(myArray) {
return myArray.toSorted();
// const arr = [...myArray];
// return arr.sort();
}
/**
* Merge Sort
* Mutates the original array with the results from the Non Mutating Merge Javascript
* @param {Array} arr - The input array to be sorted.
* @return {Array} - The sorted array.
* @complexity O(n²)
* @author Joshua Jarman
*/
export function javascriptSort(arr) {
const newArr = [...javascriptSortNonMutating(arr)];
arr.length = 0;
arr.push(...newArr);
return arr;
}