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.
40 lines
744 B
40 lines
744 B
/**
|
|
*
|
|
*/
|
|
var Selectionsort = function() {
|
|
this.instructions = [];
|
|
};
|
|
|
|
Selectionsort.prototype = Object.create(Sorter.prototype);
|
|
|
|
/**
|
|
*
|
|
*/
|
|
Selectionsort.prototype.sort = function(arr) {
|
|
var len = arr.length;
|
|
var i;
|
|
var j;
|
|
var tmp;
|
|
var swaps = 0;
|
|
var comparisons = 0;
|
|
var min;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
min = i;
|
|
for (j = i + 1; j < len; j++) {
|
|
comparisons++;
|
|
if (arr[j].value < arr[i].value) {
|
|
min = j;
|
|
}
|
|
}
|
|
|
|
if (min !== i) {
|
|
swaps++;
|
|
tmp = arr[i];
|
|
arr[i] = arr[min];
|
|
arr[min] = tmp;
|
|
}
|
|
}
|
|
|
|
console.info(`swaps: ${swaps}, comparisons: ${comparisons} `);
|
|
};
|
|
|