-
-
Notifications
You must be signed in to change notification settings - Fork 359
Add mergesort algorithm chapter in javascript #763
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
Open
jonastg
wants to merge
3
commits into
algorithm-archivists:main
Choose a base branch
from
jonastg:merge_sort_in_javascript
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−0
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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. | ||
jonastg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
{% 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.