Libslm Sort is designed to let you tinker with sorting in a variety of ways, offering full control over the comparison function, the range of the array you’re sorting, and even the algorithm you want to use. Whether you're optimizing code, trying to understand sorting techniques, or just procrastinating by playing with data.
⚠️ Use at your own risk.
ℹ️ This library is built with curiosity in mind. It's stable-ish, but don't use it in production unless you want to embrace the chaos.
- Multiple sorting algorithms implemented from scratch
- Plug-and-play comparison logic (
CompareFunction) - Custom sort ranges (
start,end) - Benchmarked and battle-tested (by one very bored developer)
| Algorithm | Stable(1) | Best | Avarage | Worst | Avarage Time(2) |
|---|---|---|---|---|---|
| Bubblesort | ✅ | n | n2 | n2 | ~176.84 ms |
| Heapsort | 🔲 | n log n | n log n | n log n | ~1.27 ms |
| Insertionsort | ✅ | n | n2 | n2 | ~128.91 ms |
| Introsort | 🔲 | n log n | n log n | n log n | ~0.99 ms |
| Mergesort | ✅ | n log n | n log n | n log n | ~1.83 ms |
| Quicksort | 🔲 | n log n | n log n | n2 | ~1.19 ms |
| Selectionsort | 🔲 | n2 | n2 | n2 | ~79.68 ms |
| Shellsort | 🔲 | n log n | n log n | n4/3 | ~1.78 ms |
| Timsort | ✅ | n | n log n | n log n | ~0.92 ms |
(1) A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input array to be sorted.
(2) Avarage time is calculated by running 10,000 random arrays, and taking the medien of the results.
import { quicksort, CompareFunction } from 'libslm-sort';
const data = [5, 3, 8, 1, 2];
const compare: CompareFunction = (a, b) => {
if (a > b) return 1;
if (a < b) return -1;
return 0;
};
const sorted = quicksort(data, 0, data.length - 1, compare);
console.log(sorted); // [1, 2, 3, 5, 8]- ✅ Initial release with working algorithms
- 🔲 Better error messages and typings
- 🔲 Add unit tests
- 🔲 Visualization tool (maybe...)
Since this library is experimental, these implementations may change or expand over time. Use them as needed, tweak them as desired, and embrace the chaos.
- All of the sorting functions are written from scratch.
- They support custom ranges and comparison logic.
- You can help! Feel free to open issues or PRs.