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