You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.3 KiB
61 lines
1.3 KiB
/**
|
|
*
|
|
*/
|
|
var BubbleSort = function() {
|
|
//===== Inits.
|
|
this.actions = [];
|
|
|
|
this.swaps = 0;
|
|
this.comparisons = 0;
|
|
|
|
//===== Action management.
|
|
};
|
|
|
|
BubbleSort.prototype = Object.create(Sorter.prototype);
|
|
|
|
/**
|
|
*
|
|
*/
|
|
BubbleSort.prototype.init = function() {
|
|
var len = this.shuffled.length;
|
|
|
|
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)
|
|
};
|
|
|
|
/**
|
|
*
|
|
*/
|
|
BubbleSort.prototype.sort = function(arr) {
|
|
var i;
|
|
var swapped = false;
|
|
var len = arr.length;
|
|
|
|
// NOTE this starts at the beginning of the array each time
|
|
for (i = 1; i < len; i++) {
|
|
if (arr[i - 1] > arr[i]) {
|
|
this.swap(arr, i, i - 1);
|
|
swapped = true;
|
|
// console.log(dump(arr));
|
|
}
|
|
}
|
|
|
|
if (swapped === true) {
|
|
this.sort(arr);
|
|
}
|
|
|
|
return arr;
|
|
};
|
|
|