settimeout-sort v1.0.1
Set Timeout Sort
Best sort. Best complexity. Please use in all of your production applications.
npm i settimeout-sort
Strictly-Sorted Usage
With Callbacks
setTimeoutSortStrict([4, 3, 2, -100, 100], (sortedList) => console.log(sortedList));
With Async
(async() => {
console.log(await setTimeoutSortStrictAsync([4, 3, 2, -100, 100]));
})();
Partially-Sorted Usage
With Callbacks
setTimeoutSort([4, 3, 2, -100, 100], (sortedList) => console.log(sortedList));
With Async
(async() => {
console.log(await setTimeoutSortAsync([4, 3, 2, -100, 100]));
})();
Caveats
This sort only works with numbers.
Your array might not end up sorted with setTimeoutSortAsync
or setTimeoutSort
. Please use setTimeoutSortStrict
or setTimeoutSortStrictAsync
to guarentee sort results.
console.log(await setTimeoutSortAsync([4, 3, 2, -100, 100]));
// 1st try [ -100, 2, 3, 4, 100 ]
// 2nd try [ -100, 2, 3, 4, 100 ]
// 3rd try [ -100, 2, 4, 3, 100 ]
Time complexity:
setTimeoutSort
:
Worst Case: O(N*T), where T is largest value in your list.
Average Case: o(N*T), where T is largest value in your list.
setTimeoutSortStrict
:
Worst Case: O(N*T + N^2), where T is largest value in your list. Insertion sort's worst-case time-complexity is O(N^2). However, it's highly unlikely that this case is ever encountered.
Average Case: o(N*T), where T is largest value in your list. Insertion sort is proven to be o(N) on partially-sorted arrays, and therefore does not increase the complexity of the algorithm.
Space complexity:
O(N). This is not an inplace-sort.