Selection sort visualization finished.

master
ben-burlingham 10 years ago
parent 89c8909ce9
commit 21357f575d
  1. 2
      index.html
  2. 8
      js/insertionsort.js
  3. 76
      js/selectionsort.js

@ -51,7 +51,7 @@
<h1>Selection sort discussion</h1>
http://stackoverflow.com/questions/15799034/insertion-sort-vs-selection-sort <br>
swap. highlight. fade.
finds upstram minimum and swaps with current.
<div class="sorter"
data-algorithm='selection'
data-stable='Maybe'

@ -48,13 +48,6 @@ var InsertionSort = function() {
}
};
//
this.noswap = function(arr, j) {
this
.instruct(Itemgroup.message, 0, 0, 3, ``)
.instruct(Itemgroup.message, 0, 100, 5, `${arr[j]} > ${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;
}
}

@ -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;
};

Loading…
Cancel
Save