* MergeSort.js
/**
* Created by Mch on 9/15/18.
*/
function MergeSort() {}
Array.prototype.mergeSort = function() {
var length = this.length, a = new Array(length);
if (length < 2) {
return this;
}
// copy
for (var i = 0; i < this.length; i++) {
a[i] = this[i];
}
Array._mergeSort(a, 0, length-1);
return a;
};
Array._mergeSort = function(/* Array */a, /* Number */left, /* Number */right) {
if (left >= right) {
return;
}
var mid = Math.floor((left - right) /2 + right);
Array._mergeSort(a, left, mid);
Array._mergeSort(a, mid+1, right);
Array.merge(a, left, mid, right);
};
/**
* left ... mid mid+1 ... right
* @param array Array
* @param left Integer
* @param mid Integer
* @param right Integer
*/
Array.merge = function(array, left, mid, right) {
var result = [], il = left, ir = mid+1;
while (il <= mid && ir <= right) {
// compare
if (array[il] < array[ir]) {
result.push(array[il++]);
} else {
result.push(array[ir++]);
}
}
while (il <= mid) {
result.push(array[il++]);
}
while (ir <= right) {
result.push(array[ir++]);
}
// put back
for (var i = left; i <= right; i++) {
array[i] = result.shift();
}
};
Array.prototype.swap = function(i, j) {
if (0<=i && i < this.length && 0 <=j && j < this.length) {
var t = this[i];
this[i] = this[j];
this[j] = t;
} else {
throw RangeException('index out of range');
}
};
Array.prototype.shuffle = function() {
var m = this.length, i;
while (m) {
i = Math.floor(Math.random() * m--);
this.swap(i, m);
}
return this;
};
MergeSort.prototype.main = function () {
// var a = [0,1,2,3,4,5,6,7,8,9];
// a = a.shuffle();
// console.log(a);
var a = [8,2,9,0,1,5,6,3,7,4];
a = a.mergeSort();
console.log(a);
};
var m = new MergeSort();
m.main();