Skip to content

Commit

Permalink
mergesort
Browse files Browse the repository at this point in the history
  • Loading branch information
felixtanhm committed Mar 17, 2024
1 parent 137fde2 commit dad0963
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
10 changes: 5 additions & 5 deletions full-stack-javascript/08-recursion/functions/mergeSort.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
function split(numArr) {
function mergeSort(numArr) {
if (numArr.length === 0) return "Array is empty";
if (numArr.length === 1) return numArr;

const mid = Math.floor(numArr.length / 2);
const leftArr = numArr.slice(0, mid);
const rightArr = numArr.slice(mid, numArr.length);

return mergeSort(split(leftArr), split(rightArr));
return sort(mergeSort(leftArr), mergeSort(rightArr));
}

function mergeSort(arr1, arr2) {
function sort(arr1, arr2) {
if (arr1 === undefined) return arr2;
if (arr2 === undefined) return arr1;

Expand All @@ -18,7 +18,7 @@ function mergeSort(arr1, arr2) {
let r = 0;

while (l < arr1.length && r < arr2.length) {
if (arr1[l] < arr2[r]) {
if (arr1[l] <= arr2[r]) {
result.push(arr1[l]);
l++;
} else {
Expand All @@ -40,4 +40,4 @@ function mergeSort(arr1, arr2) {
return result;
}

console.log(split([5, 3, 4, 1, 2]));
export { mergeSort };
67 changes: 67 additions & 0 deletions full-stack-javascript/08-recursion/tests/mergeSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { mergeSort } from "../functions/mergeSort";

describe("MergeSort", () => {
test("Empty array", () => {
const list = [];
expect(mergeSort(list)).toStrictEqual("Array is empty");
});

test("Array with a single element", () => {
const list = [-1];
expect(mergeSort(list)).toStrictEqual(list);
});

test("Already sorted array", () => {
const list = [-1, 2, 4, 5];
expect(mergeSort(list)).toStrictEqual(list);
});

test("Array with positive and negative integers", () => {
const list = [1, -10, 3, -1, 100, 5, 2, 1];
const result = [-10, -1, 1, 1, 2, 3, 5, 100];
expect(mergeSort(list)).toStrictEqual(result);
});

test("Array of zeroes", () => {
const list = [0, 0, 0, -0];
const result = [0, 0, 0, -0];
expect(mergeSort(list)).toStrictEqual(result);
});

test("Array of alphabets", () => {
const list = ["c", "z", "e", "k", "a"];
const result = ["a", "c", "e", "k", "z"];
expect(mergeSort(list)).toStrictEqual(result);
});

test("Array of integers as strings", () => {
const list = ["10", "01", "-1", "0", "6"];
const result = ["-1", "0", "01", "10", "6"];
// list should get sorted by ASCII value not numerical value
expect(mergeSort(list)).toStrictEqual(result);
});

test("Array with non-exact numbers", () => {
const list = [0.11, 0.53, 5, -0.99];
const result = [-0.99, 0.11, 0.53, 5];
expect(mergeSort(list)).toStrictEqual(result);
});

test("Large array of random integers", () => {
function getRandomArray(n, max = 40) {
const sign = [-1, 1];
return Array(n)
.fill()
.map(
() =>
sign[Math.floor(Math.random() * 2)] *
Math.round(Math.random() * max)
);
}
//create random array
const size = 99999 + parseInt(Math.random() * 1000, 10);
const list = getRandomArray(size, 10000);
const mySortedList = mergeSort(list);
expect(mySortedList).toStrictEqual(list.sort((a, b) => a - b));
});
});

0 comments on commit dad0963

Please sign in to comment.