|
|
|
@ -9,6 +9,63 @@ var SelectionSort = function() { |
|
|
|
|
this.comparisons = 0; |
|
|
|
|
|
|
|
|
|
//===== Action management.
|
|
|
|
|
//
|
|
|
|
|
this.preSort = function(arr) { |
|
|
|
|
this |
|
|
|
|
.instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0) |
|
|
|
|
|
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 1, `Comparisons: ${this.comparisons}`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 2, `Swaps: ${this.swaps}`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 3, ``) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 4, ``) |
|
|
|
|
.instruct(Itemgroup.message, 0, 100, 5, ``) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
this.findmin = function(arr, i, j, min) { |
|
|
|
|
this |
|
|
|
|
.instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0) |
|
|
|
|
// .instruct(Itemgroup.background, 1, 0, i, i, '#0C1E42')
|
|
|
|
|
.instruct(Itemgroup.background, 1, 0, j, j, Visualizer.bg1) |
|
|
|
|
.instruct(Itemgroup.background, 1, 0, i, i, '#435C11') |
|
|
|
|
|
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 1, `Comparisons: ${this.comparisons}`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 3, `Current value: ${arr[i]}`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 4, `Current minimum: ${arr[min]}`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 100, 5, `Comparing: ${arr[min]} and ${arr[j]}`) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
this.newmin = function(arr, min) { |
|
|
|
|
this |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 4, `New minimum: ${arr[min]}`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 100, 5, ``) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
this.swap = function(arr, i, min) { |
|
|
|
|
this |
|
|
|
|
.instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0) |
|
|
|
|
.instruct(Itemgroup.background, 1, 0, i, i, Visualizer.bg1) |
|
|
|
|
.instruct(Itemgroup.background, 1, 0, min, min, Visualizer.bg1) |
|
|
|
|
|
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 2, `Swaps: ${this.swaps}`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 4, ``) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 5, `Swap current and minimum.`) |
|
|
|
|
.instruct(Itemgroup.swap, 1, 300, i, min) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
this.postSort = function(arr) { |
|
|
|
|
this |
|
|
|
|
.instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0) |
|
|
|
|
|
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 1, `Comparisons: ${this.comparisons}`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 2, `Swaps: ${this.swaps}`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 3, ``) |
|
|
|
|
.instruct(Itemgroup.message, 0, 0, 4, ``) |
|
|
|
|
.instruct(Itemgroup.message, 0, 100, 5, `Sorting complete.`) |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
SelectionSort.prototype = Object.create(Sorter.prototype); |
|
|
|
@ -40,34 +97,37 @@ SelectionSort.prototype.init = function() { |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
this.preSort(arr); |
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) { |
|
|
|
|
min = i; |
|
|
|
|
for (j = i + 1; j < len; j++) { |
|
|
|
|
comparisons++; |
|
|
|
|
if (arr[j] < arr[i]) { |
|
|
|
|
this.comparisons++; |
|
|
|
|
this.findmin(arr, i, j, min); |
|
|
|
|
|
|
|
|
|
if (arr[j] < arr[min]) { |
|
|
|
|
min = j; |
|
|
|
|
this.newmin(arr, min); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (min !== i) { |
|
|
|
|
swaps++; |
|
|
|
|
this.swaps++; |
|
|
|
|
tmp = arr[i]; |
|
|
|
|
arr[i] = arr[min]; |
|
|
|
|
arr[min] = tmp; |
|
|
|
|
|
|
|
|
|
this.swap(arr, i, min); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// console.info(`swaps: ${swaps}, comparisons: ${comparisons} `);
|
|
|
|
|
this.postSort(arr); |
|
|
|
|
|
|
|
|
|
return arr; |
|
|
|
|
}; |
|
|
|
|