Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mergesort algorithm chapter in javascript #763

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions contents/merge_sort/code/javascript/mergesort.js
Original file line number Diff line number Diff line change
@@ -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"]
69 changes: 69 additions & 0 deletions contents/merge_sort/merge_sort.md
Original file line number Diff line number Diff line change
@@ -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 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" %}
[import:1-11, lang:"javascript"](code/javascript/mergesort.js)
{% endmethod %}

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" %}
[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.

<pre>
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
</pre>

## Example Code

{% method %}
{% sample lang="js" %}
[import:31-38, lang:"javascript"](code/javascript/mergesort.js)
{% endmethod %}

<script>
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>

## 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).

[<p><img class="center" src="../cc/CC-BY-SA_icon.svg" /></p>](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