From 6a09ee9da3cd9cd0b7b3306b1100a19af5f1c8ac Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Fri, 26 Apr 2024 16:19:33 +0100 Subject: [PATCH 1/5] feat: create boilerplate for exercise merge sort --- 22_mergeSort/README.md | 3 +++ 22_mergeSort/mergeSort.js | 6 ++++++ 22_mergeSort/mergeSort.spec.js | 15 +++++++++++++++ 22_mergeSort/solution/mergeSort-solution.js | 6 ++++++ 22_mergeSort/solution/mergeSort-solution.spec.js | 15 +++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 22_mergeSort/README.md create mode 100644 22_mergeSort/mergeSort.js create mode 100644 22_mergeSort/mergeSort.spec.js create mode 100644 22_mergeSort/solution/mergeSort-solution.js create mode 100644 22_mergeSort/solution/mergeSort-solution.spec.js diff --git a/22_mergeSort/README.md b/22_mergeSort/README.md new file mode 100644 index 00000000000..5b92a2b6c68 --- /dev/null +++ b/22_mergeSort/README.md @@ -0,0 +1,3 @@ +# Exercise 13 - mergeSort + +Description of the exercise goes here. diff --git a/22_mergeSort/mergeSort.js b/22_mergeSort/mergeSort.js new file mode 100644 index 00000000000..d64e7a79205 --- /dev/null +++ b/22_mergeSort/mergeSort.js @@ -0,0 +1,6 @@ +const mergeSort = function() { + +}; + +// Do not edit below this line +module.exports = mergeSort; diff --git a/22_mergeSort/mergeSort.spec.js b/22_mergeSort/mergeSort.spec.js new file mode 100644 index 00000000000..67a35b5bdf3 --- /dev/null +++ b/22_mergeSort/mergeSort.spec.js @@ -0,0 +1,15 @@ +const mergeSort = require('./mergeSort'); + +describe('mergeSort', () => { + test('First test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(mergeSort()).toBe(''); + }); + + test.skip('Second test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(mergeSort()).toBe(''); + }); +}); diff --git a/22_mergeSort/solution/mergeSort-solution.js b/22_mergeSort/solution/mergeSort-solution.js new file mode 100644 index 00000000000..21c67a0ee46 --- /dev/null +++ b/22_mergeSort/solution/mergeSort-solution.js @@ -0,0 +1,6 @@ +const mergeSort = function() { + // Replace this comment with the solution code +}; + +// Do not edit below this line +module.exports = mergeSort; diff --git a/22_mergeSort/solution/mergeSort-solution.spec.js b/22_mergeSort/solution/mergeSort-solution.spec.js new file mode 100644 index 00000000000..83f01ad3b27 --- /dev/null +++ b/22_mergeSort/solution/mergeSort-solution.spec.js @@ -0,0 +1,15 @@ +const mergeSort = require('./mergeSort-solution'); + +describe('mergeSort', () => { + test('First test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(mergeSort()).toBe(''); + }); + + test('Second test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(mergeSort()).toBe(''); + }); +}); From b7e9c638d942909dba3d2639ab75a794e9c4edde Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Fri, 26 Apr 2024 20:46:14 +0100 Subject: [PATCH 2/5] feat(mergeSort): create solution --- 22_mergeSort/solution/mergeSort-solution.js | 29 ++++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/22_mergeSort/solution/mergeSort-solution.js b/22_mergeSort/solution/mergeSort-solution.js index 21c67a0ee46..9195378371b 100644 --- a/22_mergeSort/solution/mergeSort-solution.js +++ b/22_mergeSort/solution/mergeSort-solution.js @@ -1,6 +1,27 @@ -const mergeSort = function() { - // Replace this comment with the solution code -}; - +function mergeSort(array) { + if (array.length <= 1) return array; + + const midpoint = Math.floor(array.length / 2); + const leftHalf = array.slice(0, midpoint); + const rightHalf = array.slice(midpoint); + + return merge(mergeSort(leftHalf), mergeSort(rightHalf)); +} + +function merge(left, right, merged = [], leftIndex = 0, rightIndex = 0) { + if (leftIndex >= left.length && rightIndex >= right.length) return merged; + + if ( + leftIndex < left.length && + (rightIndex >= right.length || left[leftIndex] < right[rightIndex]) + ) { + merged.push(left[leftIndex]); + return merge(left, right, merged, leftIndex + 1, rightIndex); + } else { + merged.push(right[rightIndex]); + return merge(left, right, merged, leftIndex, rightIndex + 1); + } +} + // Do not edit below this line module.exports = mergeSort; From 33c27b3ca4291660ed514a014e2d1ea4e404f20d Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Fri, 26 Apr 2024 20:46:28 +0100 Subject: [PATCH 3/5] feat(mergeSort): create readme --- 22_mergeSort/README.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/22_mergeSort/README.md b/22_mergeSort/README.md index 5b92a2b6c68..cf0ac61c515 100644 --- a/22_mergeSort/README.md +++ b/22_mergeSort/README.md @@ -1,3 +1,22 @@ -# Exercise 13 - mergeSort +# Exercise 22 - mergeSort -Description of the exercise goes here. +A significant amount of computer science is dedicated to sorting data. An algorithm which uses the "divide and conquer" approach +of recursion is able to reduce a sorting problem to smaller and smaller sub-problems. + +Merge sort is one such sorting algorithm, and can be much faster than other algorithms such as bubble sort on the right data sets. + +Essentially, merge sort recurses through an array of unsorted data until it reaches its smallest sub-set, a single item. + +Of course an array with a single item is considered sorted. Merge sort then merges the single items back together in sorted order. Pretty clever! + +To understand what merge sort algorithm is doing check out these resources: +- [Watch this introductory video from Harvard's CS50x course]() +- [Watch this more detailed video explanation by David J. Malan (watch only until 1:58:33)]() +- [The concept of merging and Merge Sort - How it Works part on YouTube to give you a more formal look at this problem if you're still unclear]() +- [Play with this Merge Sort Visualizer to get a better feel for exactly what is happening during a Merge Sort.]() + +Write a function `mergeSort` that takes in an array and returns a sorted array, using a recursive merge sort methodology. Example: + +```javascript +mergeSort([3, 2, 1, 13, 8, 5, 0, 1]) // [0, 1, 1, 2, 3, 5, 8, 13] +``` From fe684a7eec211521671e42659ea536ba5262b030 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Fri, 3 May 2024 01:34:40 +0100 Subject: [PATCH 4/5] feat(merge sort): write tests --- .../solution/mergeSort-solution.spec.js | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/22_mergeSort/solution/mergeSort-solution.spec.js b/22_mergeSort/solution/mergeSort-solution.spec.js index 83f01ad3b27..56fecc7b827 100644 --- a/22_mergeSort/solution/mergeSort-solution.spec.js +++ b/22_mergeSort/solution/mergeSort-solution.spec.js @@ -1,15 +1,45 @@ const mergeSort = require('./mergeSort-solution'); describe('mergeSort', () => { - test('First test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary + test('sorts an array of numbers', () => { + expect(mergeSort([4, 1, 3, 9, 7])).toEqual([1, 3, 4, 7, 9]); + }); + + test('returns an empty array when called with an empty array', () => { + expect(mergeSort([])).toEqual([]); + }); + + test('sorts an array with duplicate elements', () => { + expect(mergeSort([5, 3, 8, 3, 2])).toEqual([2, 3, 3, 5, 8]); + }); + + test('sorts an array of negative numbers', () => { + expect(mergeSort([-3, -1, -7, -4])).toEqual([-7, -4, -3, -1]); + }); + + test('sorts an array with negative and positive numbers', () => { + expect(mergeSort([-1, 2, -3, 4, -5])).toEqual([-5, -3, -1, 2, 4]); + }); + + test('sorts an array with one element', () => { + expect(mergeSort([10])).toEqual([10]); + }); + + test('sorts an already sorted array', () => { + expect(mergeSort([1, 2, 3, 4, 5])).toEqual([1, 2, 3, 4, 5]); + }); + + test('sorts an array in descending order', () => { + expect(mergeSort([5, 4, 3, 2, 1])).toEqual([1, 2, 3, 4, 5]); + }); - expect(mergeSort()).toBe(''); + test('sorts a large array', () => { + const largeArray = Array.from({ length: 1000 }, () => Math.floor(Math.random() * 1000)); + const sortedArray = [...largeArray].sort((a, b) => a - b); + expect(mergeSort(largeArray)).toEqual(sortedArray); }); - - test('Second test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary - expect(mergeSort()).toBe(''); + test('handles array with all the same elements', () => { + expect(mergeSort([7, 7, 7, 7, 7])).toEqual([7, 7, 7, 7, 7]); }); }); From fdf5a687bc53c7f155a5d87ed70794f712ef6263 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Fri, 3 May 2024 01:34:49 +0100 Subject: [PATCH 5/5] feat(merge sort): add links to readme --- 22_mergeSort/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/22_mergeSort/README.md b/22_mergeSort/README.md index cf0ac61c515..52d618e0697 100644 --- a/22_mergeSort/README.md +++ b/22_mergeSort/README.md @@ -10,10 +10,10 @@ Essentially, merge sort recurses through an array of unsorted data until it reac Of course an array with a single item is considered sorted. Merge sort then merges the single items back together in sorted order. Pretty clever! To understand what merge sort algorithm is doing check out these resources: -- [Watch this introductory video from Harvard's CS50x course]() -- [Watch this more detailed video explanation by David J. Malan (watch only until 1:58:33)]() -- [The concept of merging and Merge Sort - How it Works part on YouTube to give you a more formal look at this problem if you're still unclear]() -- [Play with this Merge Sort Visualizer to get a better feel for exactly what is happening during a Merge Sort.]() +- [Watch this introductory video from Harvard's CS50x course](https://www.youtube.com/watch?v=Ns7tGNbtvV4) +- [Watch this more detailed video explanation by David J. Malan (watch only until 1:58:33)](https://youtu.be/4oqjcKenCH8?t=6248) +- [The concept of merging and Merge Sort - How it Works part on YouTube to give you a more formal look at this problem if you're still unclear](https://youtu.be/mB5HXBb_HY8) +- [Play with this Merge Sort Visualizer to get a better feel for exactly what is happening during a Merge Sort.](https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/visualize/) Write a function `mergeSort` that takes in an array and returns a sorted array, using a recursive merge sort methodology. Example: