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.
 
 
 

73 lines
1.5 KiB

/**
*
*/
var SelectionSort = function() {
//===== Inits.
this.actions = [];
this.swaps = 0;
this.comparisons = 0;
//===== Action management.
};
SelectionSort.prototype = Object.create(Sorter.prototype);
/**
*
*/
SelectionSort.prototype.init = function() {
var len = this.shuffled.length;
this
.instruct(Itemgroup.items, 0, 0, len)
.instruct(Itemgroup.items, 1, 0, len)
.instruct(Itemgroup.items, 2, 0, len)
for (var i = 0; i < len; i++) {
this.instruct(Itemgroup.text, 1, 0, i, this.shuffled[i]);
}
this
.instruct(Itemgroup.foreground, 1, 0, 0, len, Visualizer.fg0)
.instruct(Itemgroup.background, 1, 0, 0, len, Visualizer.bg0)
.instruct(Itemgroup.opacity, 0, 0, 0, len, 0)
.instruct(Itemgroup.opacity, 2, 0, 0, len, 0)
};
/**
*
*/
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] < arr[i]) {
min = j;
}
}
if (min !== i) {
swaps++;
tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
}
// console.info(`swaps: ${swaps}, comparisons: ${comparisons} `);
return arr;
};