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.
32 lines
591 B
32 lines
591 B
/**
|
|
*
|
|
*/
|
|
var BubbleSort = function() {
|
|
this.instructions = [];
|
|
};
|
|
|
|
BubbleSort.prototype = Object.create(Sorter.prototype);
|
|
|
|
/**
|
|
*
|
|
*/
|
|
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].value > arr[i].value) {
|
|
this.swap(arr, i, i - 1);
|
|
swapped = true;
|
|
// console.log(dump(arr));
|
|
}
|
|
}
|
|
|
|
if (swapped === true) {
|
|
this.sort(arr);
|
|
}
|
|
|
|
return arr;
|
|
};
|
|
|