Skip to content

Instantly share code, notes, and snippets.

@arasatasaygin
Created December 8, 2014 09:27
Show Gist options
  • Select an option

  • Save arasatasaygin/32ceff61aa4e043603ec to your computer and use it in GitHub Desktop.

Select an option

Save arasatasaygin/32ceff61aa4e043603ec to your computer and use it in GitHub Desktop.
Merge, mergesort implementation
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