Sorting Algorithms Utility
npm install @mfx2/sorting-algorithms-utilityA collection of sorting algorithms implemented in TypeScript.
``bash`
npm i @mfx2/sorting-algorithms-utility
`typescript
const sorter = new Sorter();
const unsortedArray = [64, 34, 25, 12, 22, 11, 90];
// Using Bubble Sort
const bubbleSorted = sorter.bubbleSort([...unsortedArray]);
console.log('Bubble Sort:', bubbleSorted);
// Using Merge Sort
const mergeSorted = sorter.mergeSort([...unsortedArray]);
console.log('Merge Sort:', mergeSorted);
// Using Quick Sort
const quickSorted = sorter.quickSort([...unsortedArray]);
console.log('Quick Sort:', quickSorted);
// Using Heap Sort
const heapSorted = sorter.heapSort([...unsortedArray]);
console.log('Heap Sort:', heapSorted);
// Using Selection Sort
const selectionSorted = sorter.selectionSort([...unsortedArray]);
console.log('Selection Sort:', selectionSorted);
// Using Insertion Sort
const insertionSorted = sorter.insertionSort([...unsortedArray]);
console.log('Insertion Sort:', insertionSorted);
`
- Bubble Sort: bubbleSort(array: number[]): number[]mergeSort(array: number[]): number[]
- Merge Sort: quickSort(array: number[]): number[]
- Quick Sort: heapSort(array: number[]): number[]
- Heap Sort: selectionSort(array: number[]): number[]
- Selection Sort: insertionSort(array: number[]): number[]`
- Insertion Sort:
Each sorting method takes an array of numbers as input and returns a sorted array.
The time complexity of these sorting algorithms varies:
- Bubble Sort: O(n²)
- Merge Sort: O(n log n)
- Quick Sort: O(n log n) average case, O(n²) worst case
- Heap Sort: O(n log n)
- Selection Sort: O(n²)
- Insertion Sort: O(n²)