forked from kidaa30/js-visual-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorts.BubbleSort.js
More file actions
51 lines (38 loc) · 871 Bytes
/
Sorts.BubbleSort.js
File metadata and controls
51 lines (38 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var Sorts = Sorts || {};
(function ($) {
$.BubbleSort = function(block) {
this.block = block;
this._c = block.getCount();
this._maxIndex = this._c - 1;
this.sorted = false;
this._i = 0;
this._changeFlag = false;
this._cmp = 0;
this._state = 0;
}
$.BubbleSort.prototype.step = function() {
this.block.i = this._i;
this.block.j = this._i + 1;
if (this._state == 0) {
this._state = 1;
this._cmp = this.block.less(this._i, this._i + 1);
} else {
this._state = 0;
if (this._cmp) {
this.block.exch(this._i, this._i + 1);
this._changeFlag = true;
}
this._i++;
if (this._i == this._maxIndex) {
if (this._changeFlag) {
this._i = 0;
this._changeFlag = false;
} else {
this.sorted = true;
this.block.i = -1;
this.block.j = -1;
}
}
}
};
})(Sorts);