|
| 1 | +/* Merge Sort |
| 2 | +
|
| 3 | +Description: Merge sort is a divide-and-conquer algorithm that divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves. The merge() function is used for merging two halves. |
| 4 | +
|
| 5 | +Time Complexity: O(n log n) |
| 6 | +
|
| 7 | +Space Complexity: O(n) |
| 8 | +*/ |
| 9 | + |
| 10 | +function mergeSort(arr: number[]): number[] { |
| 11 | + if (arr.length <= 1) { |
| 12 | + return arr; |
| 13 | + } |
| 14 | + |
| 15 | + const middle: number = Math.floor(arr.length / 2); |
| 16 | + const leftList: number[] = arr.slice(0, middle); |
| 17 | + const rightList: number[] = arr.slice(middle); |
| 18 | + |
| 19 | + return merge(mergeSort(leftList), mergeSort(rightList)); |
| 20 | +} |
| 21 | + |
| 22 | +function merge(leftList: number[], rightList: number[]): number[] { |
| 23 | + let result: number[] = []; |
| 24 | + let left: number = 0; |
| 25 | + let right: number = 0; |
| 26 | + |
| 27 | + while (left < leftList.length && right < rightList.length) { |
| 28 | + if (leftList[left] < rightList[right]) { |
| 29 | + result.push(leftList[left]); |
| 30 | + left++; |
| 31 | + } else { |
| 32 | + result.push(rightList[right]); |
| 33 | + right++; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return result.concat(leftList.slice(left)).concat(rightList.slice(right)); |
| 38 | +} |
| 39 | + |
| 40 | +function runTests(): void { |
| 41 | + console.log("---------- Merge Sort Test Cases ----------"); |
| 42 | + |
| 43 | + // Test 1: Regular unsorted array |
| 44 | + const test1: number[] = [64, 34, 25, 12, 22, 11, 90]; |
| 45 | + console.log("\nTest 1 - Original:", test1); |
| 46 | + console.log("Test 1 - Sorted:", mergeSort(test1)); |
| 47 | + |
| 48 | + // Test 2: Already sorted array |
| 49 | + const test2: number[] = [1, 2, 3, 4, 5]; |
| 50 | + console.log("\nTest 2 - Original:", test2); |
| 51 | + console.log("Test 2 - Sorted:", mergeSort(test2)); |
| 52 | + |
| 53 | + // Test 3: Reverse sorted array |
| 54 | + const test3: number[] = [5, 4, 3, 2, 1]; |
| 55 | + console.log("\nTest 3 - Original:", test3); |
| 56 | + console.log("Test 3 - Sorted:", mergeSort(test3)); |
| 57 | + |
| 58 | + // Test 4: Array with duplicates |
| 59 | + const test4: number[] = [3, 1, 4, 1, 5, 9, 2, 6, 5]; |
| 60 | + console.log("\nTest 4 - Original:", test4); |
| 61 | + console.log("Test 4 - Sorted:", mergeSort(test4)); |
| 62 | + |
| 63 | + // Test 5: Single element array |
| 64 | + const test5: number[] = [42]; |
| 65 | + console.log("\nTest 5 - Original:", test5); |
| 66 | + console.log("Test 5 - Sorted:", mergeSort(test5)); |
| 67 | + |
| 68 | + // Test 6: Empty array |
| 69 | + const test6: number[] = []; |
| 70 | + console.log("\nTest 6 - Original:", test6); |
| 71 | + console.log("Test 6 - Sorted:", mergeSort(test6)); |
| 72 | + |
| 73 | + // Test 7: Large array |
| 74 | + const test7: number[] = [38, 27, 43, 3, 9, 82, 10]; |
| 75 | + console.log("\nTest 7 - Original:", test7); |
| 76 | + console.log("Test 7 - Sorted:", mergeSort(test7)); |
| 77 | +} |
| 78 | + |
| 79 | +runTests(); |
0 commit comments