function ArrayList() {
let array = [5, 4, 3, 2, 1];
const merge = function (left, right) {
console.log('merge----', left, right)
let result = [];
let il = 0;
let ir = 0;
while(il < left.length && ir < right.length) {
if (left[il] < right[ir]) {
result.push(left[il++]);
} else {
result.push(right[ir++]);
}
}
while(il < left.length) {
result.push(left[il++]);
}
while(ir < right.length) {
result.push(right[ir++]);
}
console.log('merge--', result)
return result;
}
const mergeSortRec = function (array) {
console.log('---', array)
const length = array.length;
if (length === 1) {
return array;
}
const mid = Math.floor(length / 2);
const left = array.slice(0, mid);
const right = array.slice(mid, length);
return merge(mergeSortRec(left), mergeSortRec(right));
}
this.mergeSort = function() {
array = mergeSortRec(array)
return array
}
}
const al = new ArrayList()
console.log(al.mergeSort())
console