diff --git a/index.html b/index.html
index abedbce..203a17d 100644
--- a/index.html
+++ b/index.html
@@ -51,7 +51,7 @@
Selection sort discussion
http://stackoverflow.com/questions/15799034/insertion-sort-vs-selection-sort
- swap. highlight. fade.
+ finds upstram minimum and swaps with current.
${arr[j - 1]}, not swapped.`)
- };
-
//
this.postSort = function(arr) {
this
@@ -118,7 +111,6 @@ InsertionSort.prototype.sort = function(arr) {
this.swap(arr, j);
}
else {
- this.noswap(arr, j);
break;
}
}
diff --git a/js/selectionsort.js b/js/selectionsort.js
index 2727b96..d9db13b 100644
--- a/js/selectionsort.js
+++ b/js/selectionsort.js
@@ -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;
};