Here you can find the source of mergeSort(array)
function mergeSort(array) { var merge = function (left, right) { var total = [] while (left.length && right.length) total.push(left[0] <= right[0] ? left.shift() : right.shift()) return total.concat(left.concat(right)); }/*w w w .j av a2 s . c om*/ var length = array.length; if (length < 2) return array var left = array.slice(0, parseInt(length / 2)); var right = array.slice(parseInt(length / 2)); return merge(mergeSort(left), mergeSort(right)); } var array = [121,65,12,2,1,6,56,3,54,4] var array1 = mergeSort(array) console.log(array1) Array.prototype.merge_sort = function() { var merge = function(left, right) { var final = []; while (left.length && right.length) final.push(left[0] <= right[0] ? left.shift() : right.shift()); return final.concat(left.concat(right)); }; var len = this.length; if (len < 2) return this; var mid = len / 2; return merge(this.slice(0, parseInt(mid)).merge_sort(), this.slice(parseInt(mid)).merge_sort()); };
var mergeSort = function (arr) { var n = parseInt(arr.length/2); if(n < 1){ return arr; }else{ var arr1 = mergeSort(arr.slice(0, n)); var arr2 = mergeSort(arr.slice(n)); var a = []; ...
'use strict'; Array.prototype.swap = function (i, j) { const temp = this[i]; this[i] = this[j]; this[j] = temp; function mergesort (arr, lo, hi) { function partition (lo, hi) { if (lo >= hi) return; ...
function merge(left, right) { var result = []; while(left.length > 0 || right.length > 0) { if(left.length > 0 && right.length > 0) { if(left[0] <= right[0]) { result.push(left.shift()); } else { result.push(right.shift()); } else if(left.length > 0) { result.push(left.shift()); } else if(right.length > 0) { result.push(right.shift()); return result; function mergesort(A) { if(A.length < 2) { return A; var middle = Math.ceil(A.length / 2); var left = A.splice(0, middle); var right = A; left = mergesort(left); right = mergesort(right); return merge(left, right); Array.prototype.mergeSort = function() { return mergesort(this);
var arr = [2, 4, 6, 1, 3, 5, 8, 7]; var mergesort = function (lst) { if (lst.length === 1) return lst; var L = lst.slice(0, lst.length / 2), R = lst.slice(lst.length / 2, lst.length); var left = mergesort(L), right = mergesort(R); return merge(left, right); function merge(left, right) { ...
function merge(left, right) { const merged = []; while (left.length > 0 && right.length > 0) { let nextItem = (left[0] < right[0]) ? left.shift() : right.shift(); merged.push(nextItem); return merged.concat(left, right); function mergeSort(array) { ...
Array.prototype.mergeSort = function(){ var merge = function(left,right){ var merged = []; while(left.length && right.length){ merged.push(left[0] <= right[0] ? left.shift():right.shift()); return merged.concat(left.concat(right)); if (this.length < 2) return this; ...
Array.prototype.mergeSort = function(){ var merge = function(left,right){ var merged = []; while(left.length && right.length){ merged.push(left[0] <= right[0] ? left.shift():right.shift()); return merged.concat(left.concat(right)); var myarray = []; ...
Array.prototype.mergeSort = function() { function merge(left, right) { var arr = [] while(left.length && right.length) { if(left[0] < right[0]) { arr.push(left.shift()) } else { arr.push(right.shift()) return arr.concat(left, right) var len = this.length if(len > 1) { var index = Math.floor(len/2) var left = this.slice(0, index) var right = this.slice(index) return merge(left.mergeSort(), right.mergeSort()) } else { return this