/** * */ var SelectionSort = function() { //===== 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, `Starting sort.`) }; // this.midSort = function(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]}`) }; // 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); /** * */ 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.preSort(arr); for (i = 0; i < len; i++) { min = i; for (j = i + 1; j < len; j++) { this.comparisons++; this.midSort(arr, i, j, min); if (arr[j] < arr[min]) { min = j; this.newmin(arr, min); } } if (min !== i) { this.swaps++; tmp = arr[i]; arr[i] = arr[min]; arr[min] = tmp; this.swap(arr, i, min); } } this.postSort(arr); return arr; };