diff --git a/src/sort/merge/mergeSort.spec.ts b/src/sort/merge/mergeSort.spec.ts new file mode 100644 index 0000000..889bf85 --- /dev/null +++ b/src/sort/merge/mergeSort.spec.ts @@ -0,0 +1,39 @@ +import mergeSort from './mergeSort'; + +const sortLowToHigh = (a: number, b: number): boolean => (a > b); +const sortHighToLow = (a: number, b: number): boolean => (a < b); + +describe('Merge sort', () => { + it('should return an empty array when the array for sorting is empty', () => { + const arrayToSort: number[] = []; + const expectedArray: number[] = []; + expect(mergeSort(arrayToSort, sortLowToHigh)) + .toEqual(expectedArray); + }); + + it('should return an array with the element', () => { + const arrayToSort: number[] = [14]; + const expectedArray: number[] = [14]; + expect(mergeSort(arrayToSort, sortLowToHigh)) + .toEqual(expectedArray); + }); + + it('should return an array with two elements sorted from lowest to highest', () => { + const arrayToSort = [65, 43]; + const expectedArray = [43, 65]; + expect(mergeSort(arrayToSort, sortLowToHigh)) + .toEqual(expectedArray); + }); + + it('should return an array sorted from lowest to highest', () => { + const arrayToSort = [43, 65, 44, 12, 67, 1, 9, 33, 21]; + const expectedArray = [1, 9, 12, 21, 33, 43, 44, 65, 67]; + expect(mergeSort(arrayToSort, sortLowToHigh)).toEqual(expectedArray); + }); + + it('should return an array sorted from highest to lowest', () => { + const arrayToSort = [43, 65, 44, 12, 67, 1, 9, 33, 21]; + const expectedArray = [67, 65, 44, 43, 33, 21, 12, 9, 1]; + expect(mergeSort(arrayToSort, sortHighToLow)).toEqual(expectedArray); + }); +}); diff --git a/src/sort/merge/mergeSort.ts b/src/sort/merge/mergeSort.ts new file mode 100644 index 0000000..616d457 --- /dev/null +++ b/src/sort/merge/mergeSort.ts @@ -0,0 +1,42 @@ +interface CompareFunction { + (a: number, b: number): boolean; +} + +function merge(a: number[], b: number[], compareFunction: CompareFunction) { + const arrayC: number[] = []; + while (a[0] && b[0]) { + if (compareFunction(a[0], b[0])) { + arrayC.push((b).shift() as number); + } else { + arrayC.push(a.shift() as number); + } + } + while (a[0]) { + arrayC.push(a.shift() as number); + } + while (b[0]) { + arrayC.push(b.shift() as number); + } + return arrayC; +} + +export default function mergeSort(array: number[], compareFunction:CompareFunction): number[] { + if (!array.length) { + return []; + } + const introducedArray = array; + if (introducedArray.length === 1) { + return introducedArray; + } + let arrayA: number[] = []; + for (let i = 0; i < Math.trunc(introducedArray.length / 2); i += 1) { + arrayA.push(introducedArray[i]); + } + arrayA = mergeSort(arrayA, compareFunction); + let arrayB: number[] = []; + for (let i = Math.trunc(introducedArray.length / 2); i < introducedArray.length; i += 1) { + arrayB.push(introducedArray[i]); + } + arrayB = mergeSort(arrayB, compareFunction); + return merge(arrayA, arrayB, compareFunction); +}