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.
 
 
 

44 lines
813 B

/**
*
*/
var SelectionSort = function() {
this.instructions = [];
};
SelectionSort.prototype = Object.create(Sorter.prototype);
/**
*
*/
SelectionSort.prototype.sort = function(arr) {
console.error('selection sort is broken.');
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} `);
return arr;
};