From 4bab5bda78b915fc7054446205bf66f06371f3fa Mon Sep 17 00:00:00 2001 From: Jonas Talavera Date: Sat, 10 Oct 2020 19:41:26 +0200 Subject: [PATCH 1/3] Add mergesort algorithm in javascript --- SUMMARY.md | 1 + .../merge_sort/code/javascript/mergesort.js | 38 ++++++++++ contents/merge_sort/merge_sort.md | 69 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 contents/merge_sort/code/javascript/mergesort.js create mode 100644 contents/merge_sort/merge_sort.md diff --git a/SUMMARY.md b/SUMMARY.md index 270509494..01358bdff 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -15,6 +15,7 @@ * [Sorting and Searching](contents/sorting_and_searching/sorting_and_searching.md) * [Bubble Sort](contents/bubble_sort/bubble_sort.md) * [Bogo Sort](contents/bogo_sort/bogo_sort.md) + * [Merge Sort](contents/merge_sort/merge_sort.md) * [Tree Traversal](contents/tree_traversal/tree_traversal.md) * [Euclidean Algorithm](contents/euclidean_algorithm/euclidean_algorithm.md) * [Monte Carlo](contents/monte_carlo_integration/monte_carlo_integration.md) diff --git a/contents/merge_sort/code/javascript/mergesort.js b/contents/merge_sort/code/javascript/mergesort.js new file mode 100644 index 000000000..dcd4efbea --- /dev/null +++ b/contents/merge_sort/code/javascript/mergesort.js @@ -0,0 +1,38 @@ +function mergeSort(array) { + if (array.length <= 1) { + return array; + } + + const middle = Math.floor(array.length / 2); + const leftHalf = array.slice(0, middle); + const rightHalf = array.slice(middle); + + return merge(mergeSort(leftHalf), mergeSort(rightHalf)); +} + +function merge(left, right) { + const sorted = []; + let indexLeft = 0; + let indexRight = 0; + + while (indexLeft < left.length && indexRight < right.length) { + if (left[indexLeft] < right[indexRight]) { + sorted.push(left[indexLeft]); + indexLeft++; + } else { + sorted.push(right[indexRight]); + indexRight++; + } + } + + return sorted.concat(left.slice(indexLeft), right.slice(indexRight)); +} + +const unsortedText = ['M','E','R','G','E','S','O','R','T','E','X','A','M','P','L','E']; +const sortedText = mergeSort(unsortedText); + +console.log(unsortedText); +console.log(sortedText); + +// input: ['M', 'E', 'R', 'G', 'E', 'S', 'O', 'R', 'T', 'E', 'X', 'A', 'M', 'P', 'L', 'E'] +// result: ["A", "E", "E", "E", "E", "G", "L", "M", "M", "O", "P", "R", "R", "S", "T", "X"] diff --git a/contents/merge_sort/merge_sort.md b/contents/merge_sort/merge_sort.md new file mode 100644 index 000000000..f73e45e1f --- /dev/null +++ b/contents/merge_sort/merge_sort.md @@ -0,0 +1,69 @@ +# Merge Sort +Merge sort or mergesort is one of the most efficient and popular sorting algorithms today, it was invented by John von Neumann in 1945. It works on the principle of divide and conquer. That means, it will divide the problem into smaller problems and then solve each of these small problems in order to solve the whole problem. In other words, merge sort works by dividing the unordered array of elements into two halves and sorting them, which in turn it will split again in two halves and sort (recursively), then all the results will be merged resulting in a sorted array. + +Merge sort guarantees to sort an array of N items in time proportional to $$\mathcal{O}(nLogn)$$, no matter what the input. It’s good to keep in mind that it works better with larger amounts of data than small sets, in which case should be considered another algorithm, like insertion sort. Another caracteritics is that it's a stable sort which means that the same element in an array maintain their original positions with respect to each other. + +How does it work? This implementation is known as top-down implementation, the array is splitted into two parts that are used to call mergesort recursively. The result of these calls is merged into a sorted array and returnted to the upper call. + +{% method %} +{% sample lang="js" %} +[import:1-11, lang:"javascript"](code/javascript/mergesort.js) +{% endmethod %} + +The merge part of the algorithm is responsible of go over the two parts to fill up the resulting array with the elements sorted. + +{% method %} +{% sample lang="js" %} +[import:13-29, lang:"javascript"](code/javascript/mergesort.js) +{% endmethod %} + +This is an example of how merge sort works over every iteration until return the result. + +
+    0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
+    ----------------------------------------------
+ 0: M  E  R  G  E  S  O  R  T  E  X  A  M  P  L  E
+ 1: E  M
+ 2:       G  R
+ 3: E  G  M  R
+ 4:             E  S
+ 5:                   O  R
+ 6:             E  O  R  S
+ 7: E  E  G  M  O  R  R  S
+ 8:                         E  T
+ 9:                               A  X
+10:                         A  E  T  X
+11:                                     M  P
+12:                                           E  L
+13:                                     E  L  M  P
+14:                         A  E  E  L  M  P  T  X
+15: A  E  E  E  E  G  L  M  M  O  P  R  R  S  T  X
+
+ +## Example Code + +{% method %} +{% sample lang="js" %} +[import:31-38, lang:"javascript"](code/javascript/mergesort.js) +{% endmethod %} + + + +## License + +##### Code Examples + +The code examples are licensed under the MIT license (found in [LICENSE.md](https://github.com/algorithm-archivists/algorithm-archive/blob/master/LICENSE.md)). + +##### Text + +The text of this chapter was written by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode). + +[

](https://creativecommons.org/licenses/by-sa/4.0/) + +##### Pull Requests + +After initial licensing ([#560](https://github.com/algorithm-archivists/algorithm-archive/pull/560)), the following pull requests have modified the text or graphics of this chapter: +- none From 059f0c72179dfff6836bf7f867d113c5e1d58cde Mon Sep 17 00:00:00 2001 From: Jonas Talavera Date: Thu, 7 Jan 2021 08:54:06 +0100 Subject: [PATCH 2/3] Update contents/merge_sort/merge_sort.md Co-authored-by: Nufflee --- contents/merge_sort/merge_sort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/merge_sort/merge_sort.md b/contents/merge_sort/merge_sort.md index f73e45e1f..c462152b3 100644 --- a/contents/merge_sort/merge_sort.md +++ b/contents/merge_sort/merge_sort.md @@ -3,7 +3,7 @@ Merge sort or mergesort is one of the most efficient and popular sorting algorit Merge sort guarantees to sort an array of N items in time proportional to $$\mathcal{O}(nLogn)$$, no matter what the input. It’s good to keep in mind that it works better with larger amounts of data than small sets, in which case should be considered another algorithm, like insertion sort. Another caracteritics is that it's a stable sort which means that the same element in an array maintain their original positions with respect to each other. -How does it work? This implementation is known as top-down implementation, the array is splitted into two parts that are used to call mergesort recursively. The result of these calls is merged into a sorted array and returnted to the upper call. +How does it work? This implementation is known as top-down implementation, the array is split into two parts that are used to call mergesort recursively. The result of these calls is merged into a sorted array and returned to the upper call. {% method %} {% sample lang="js" %} From 6531d7e4c76b92eeb785d71a90979dcc789f68fd Mon Sep 17 00:00:00 2001 From: Jonas Talavera Date: Thu, 7 Jan 2021 08:54:22 +0100 Subject: [PATCH 3/3] Update contents/merge_sort/merge_sort.md Co-authored-by: Nufflee --- contents/merge_sort/merge_sort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/merge_sort/merge_sort.md b/contents/merge_sort/merge_sort.md index c462152b3..86d42323e 100644 --- a/contents/merge_sort/merge_sort.md +++ b/contents/merge_sort/merge_sort.md @@ -10,7 +10,7 @@ How does it work? This implementation is known as top-down implementation, the a [import:1-11, lang:"javascript"](code/javascript/mergesort.js) {% endmethod %} -The merge part of the algorithm is responsible of go over the two parts to fill up the resulting array with the elements sorted. +The merge part of the algorithm is responsible of going over the two parts to fill up the resulting array with the sorted elements. {% method %} {% sample lang="js" %}