Bubble sort visualization finished.

master
ben-burlingham 10 years ago
parent 21357f575d
commit f0e8fe4f44
  1. 2
      index.html
  2. 80
      js/bubblesort.js
  3. 2
      js/insertionsort.js
  4. 2
      js/mergesort.js
  5. 2
      js/quicksort.js
  6. 7
      js/selectionsort.js
  7. 2
      js/shellsort.js
  8. 9
      js/sorter.js

@ -78,7 +78,7 @@
<h1>bubble sort discussion</h1>
how is this different from insertion and selection sorts. <br>
swap. highlight. fade.
talk about turtles and rabbits, because search loops from beginning each time. Every number out of place means a new pass must be done.
<div class="sorter"
data-algorithm='bubble'
data-stable='Maybe'

@ -9,6 +9,65 @@ var BubbleSort = 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, `Starting sort.`)
};
//
this.midSort = function(arr, i) {
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, (i - 1), (i - 1), Visualizer.bg1)
.instruct(Itemgroup.message, 0, 0, 1, `Comparisons: ${this.comparisons}`)
.instruct(Itemgroup.message, 0, 0, 3, `Comparing ${arr[i - 1]} and ${arr[i]}.`)
.instruct(Itemgroup.message, 0, 0, 4, ``)
.instruct(Itemgroup.message, 0, 100, 5, ``)
};
//
this.swap = function(arr, i) {
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, (i - 1), (i - 1), Visualizer.bg1)
.instruct(Itemgroup.message, 0, 0, 2, `Swaps: ${this.swaps}`)
.instruct(Itemgroup.message, 0, 0, 4, `${arr[i]} > ${arr[i - 1]}`)
.instruct(Itemgroup.message, 0, 0, 5, `Swap current and previous.`)
.instruct(Itemgroup.swap, 1, 300, i, i - 1)
};
//
this.foundSwapped = function(arr) {
this
.instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0)
.instruct(Itemgroup.message, 0, 0, 3, ``)
.instruct(Itemgroup.message, 0, 0, 4, `Swapped elements found.`)
.instruct(Itemgroup.message, 0, 100, 5, `Recursing.`)
};
//
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.`)
};
};
BubbleSort.prototype = Object.create(Sorter.prototype);
@ -40,22 +99,35 @@ BubbleSort.prototype.init = function() {
*
*/
BubbleSort.prototype.sort = function(arr) {
var i;
var i, tmp;
var swapped = false;
var len = arr.length;
// NOTE this starts at the beginning of the array each time
this.preSort(arr);
for (i = 1; i < len; i++) {
this.comparisons++;
this.midSort(arr, i);
if (arr[i - 1] > arr[i]) {
this.swap(arr, i, i - 1);
this.swaps++;
tmp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = tmp;
swapped = true;
// console.log(dump(arr));
this.swap(arr, i, i - 1);
}
}
if (swapped === true) {
this.foundSwapped(arr);
this.sort(arr);
}
this.postSort(arr);
return arr;
};

@ -18,7 +18,7 @@ var InsertionSort = function() {
.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, ``)
.instruct(Itemgroup.message, 0, 100, 5, `Starting sort.`)
};
//

@ -18,7 +18,7 @@ var MergeSort = function() {
.instruct(Itemgroup.message, 0, 0, 2, '')
.instruct(Itemgroup.message, 0, 0, 3, '')
.instruct(Itemgroup.message, 0, 0, 4, '')
.instruct(Itemgroup.message, 0, 0, 5, '');
.instruct(Itemgroup.message, 0, 0, 5, 'Starting sort.');
};

@ -22,7 +22,7 @@ var QuickSort = function() {
.instruct(Itemgroup.message, 0, 0, 3, 'Sorting from [' + left + '] to [' + right + ']')
.instruct(Itemgroup.message, 0, 0, 4, '')
.instruct(Itemgroup.message, 0, 100, 5, '');
.instruct(Itemgroup.message, 0, 100, 5, 'Starting sort.');
};
//

@ -18,14 +18,13 @@ var SelectionSort = function() {
.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, ``)
.instruct(Itemgroup.message, 0, 100, 5, `Starting sort.`)
};
//
this.findmin = function(arr, i, j, min) {
this.midSort = 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')
@ -109,7 +108,7 @@ SelectionSort.prototype.sort = function(arr) {
min = i;
for (j = i + 1; j < len; j++) {
this.comparisons++;
this.findmin(arr, i, j, min);
this.midSort(arr, i, j, min);
if (arr[j] < arr[min]) {
min = j;

@ -19,7 +19,7 @@ var ShellSort = function() {
.instruct(Itemgroup.message, 0, 0, 2, `Swaps: ${this.swaps}`)
.instruct(Itemgroup.message, 0, 0, 3, `Gap: ${gap}`)
.instruct(Itemgroup.message, 0, 0, 4, ``)
.instruct(Itemgroup.message, 0, 100, 5, ``)
.instruct(Itemgroup.message, 0, 100, 5, `Starting sort.`)
};
//

@ -26,15 +26,6 @@ Sorter.prototype.shuffle = function(arr) {
return result;
};
/**
*
*/
Sorter.prototype.swap = function(arr, i, j) {
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
};
/**
*
*/

Loading…
Cancel
Save