Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/sort/merge/mergeSort.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
42 changes: 42 additions & 0 deletions src/sort/merge/mergeSort.ts
Original file line number Diff line number Diff line change
@@ -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);
}