forked from kidaa30/js-visual-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorts.SelectionSort.js
More file actions
54 lines (43 loc) · 1.03 KB
/
Sorts.SelectionSort.js
File metadata and controls
54 lines (43 loc) · 1.03 KB
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
51
52
53
54
var Sorts = Sorts || {};
(function ($) {
$.SelectionSort = function(block) {
this.block = block;
this._c = block.getCount();
this._maxIndex = this._c - 1;
this._maxK = this._c;
this.sorted = false;
this._i = -1;
this._j = 0;
this._k = 0;
this._state = 0;
}
$.SelectionSort.prototype.step = function() {
this.block.i = this._j;
this.block.j = this._k;
if (this._state == 0) {
this._state = 1;
this._k++;
// when k passes last index, we know we have found the minimum
if (this._k == this._maxK) {
this._i++;
this.block.exch(this._i, this._j);
this._j = this._i + 1;
this._k = this._j;
}
// if i reaches last index, then we know array is sorted
// i only makes one trip through the array
if (this._i == this._maxIndex) {
this.sorted = true;
this.block.i = -1;
this.block.j = -1;
}
}
else if (this._state = 1) {
this._state = 0;
// found new minimum
if (this.block.less(this._j, this._k)) {
this._j = this._k;
}
}
};
})(Sorts);