You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

53 lines
919 B

/**
*
*/
var Mergesort = function() {
this.instructions = [];
};
Mergesort.prototype = Object.create(Sorter.prototype);
/**
*
*/
Mergesort.prototype.addInstruction = function(operation, left, right, mid) {
console.log({
operation: operation,
left: left,
right: right,
mid: mid
});
this.instructions.push({
operation: operation,
left: left,
right: right
});
};
var count = 0;
/**
*
*/
Mergesort.prototype.sort = function(arr, start, end) {
count++;
if (count > 30) {
console.error('count exceeded');
return;
}
if (start >= end) {
this.addInstruction('single', start, end, null);
return;
}
var mid = start + Math.floor((end - start) / 2);
// this.addInstruction('init', start, end, mid);
this.sort(arr, start, mid);
this.sort(arr, mid + 1, end);
return;
};