标签:tput slice wchar output out prototype har sam src
Write a merge sort program in JavaScript.
Sample array : [34, 7, 23, 32, 5, 62]
Sample output : [5, 7, 23, 32, 34, 62]
Pictorial Presentation:
Sample Solution:
Array.prototype.merge_Sort = function () {
if (this.length <= 1)
{
return this;
}
let half = parseInt(this.length / 2);
let left = this.slice(0, half).merge_Sort();
let right = this.slice(half, this.length).merge_Sort();
let merge = function (left, right) {
let arry = [];
while (left.length > 0 && right.length > 0) {
arry.push((left[0] <= right[0]) ? left.shift() : right.shift());
}
return arry.concat(left).concat(right);
};
return merge(left, right);
};
let arr = [34,7,23,32,5,62];
console.log(arr.merge_Sort());
Flowchart:
标签:tput slice wchar output out prototype har sam src
原文地址:https://www.cnblogs.com/PrimerPlus/p/12920577.html