/** * */ var SelectionSort = function() { var _this = this; this.ui = { // presort: function presort(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, `Starting sort.`) }, // midsort: function midsort(arr, i, j, min) { _this .instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0) .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]}`) }, // newmin: function newmin(arr, min) { _this .instruct(Itemgroup.message, 0, 0, 4, `New minimum: ${arr[min]}`) .instruct(Itemgroup.message, 0, 100, 5, ``) }, // swap: function swap(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) }, // postsort: function postsort(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); /** * */ SelectionSort.prototype.init = function() { var len = this.shuffled.length; this.actions = []; this.swaps = 0; this.comparisons = 0; 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) { var len = arr.length; var i; var j; var tmp; var min; this.ui.presort(arr); for (i = 0; i < len; i++) { min = i; for (j = i + 1; j < len; j++) { this.comparisons++; this.ui.midsort(arr, i, j, min); if (arr[j] < arr[min]) { min = j; this.ui.newmin(arr, min); } } if (min !== i) { this.swaps++; tmp = arr[i]; arr[i] = arr[min]; arr[min] = tmp; this.ui.swap(arr, i, min); } } this.ui.postsort(arr); return arr; };