Created
December 8, 2014 09:27
-
-
Save arasatasaygin/32ceff61aa4e043603ec to your computer and use it in GitHub Desktop.
Merge, mergesort implementation
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
| function merge(left, right) { | |
| var result = []; | |
| while(left.length > 0 && right.length > 0) { | |
| if(left[0] < right[0]) { | |
| result.push(left.shift()); | |
| } else { | |
| result.push(right.shift()); | |
| } | |
| } | |
| return result.concat(left).concat(right); | |
| } | |
| function mergeSort(items) { | |
| if(items.length === 1) { | |
| return items; | |
| } | |
| var middle = Math.floor(items.length / 2); | |
| var left = items.slice(0, middle); | |
| var right = items.slice(middle); | |
| return merge(mergeSort(left), mergeSort(right)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment