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.
38 lines
677 B
38 lines
677 B
/**
|
|
*
|
|
*/
|
|
var InsertionSort = function() {
|
|
this.instructions = [];
|
|
};
|
|
|
|
InsertionSort.prototype = Object.create(Sorter.prototype);
|
|
|
|
/**
|
|
*
|
|
*/
|
|
InsertionSort.prototype.sort = function(arr) {
|
|
var len = arr.length;
|
|
var i;
|
|
var j;
|
|
var tmp;
|
|
var swaps = 0;
|
|
var comparisons = 0;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
for (j = i; j > 0; j--) {
|
|
comparisons++;
|
|
|
|
if (arr[j - 1].value > arr[j].value) {
|
|
swaps++;
|
|
tmp = arr[j - 1];
|
|
arr[j - 1] = arr[j];
|
|
arr[j] = tmp;
|
|
}
|
|
else {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return arr;
|
|
};
|
|
|