-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselectionMin.js
More file actions
36 lines (33 loc) · 977 Bytes
/
selectionMin.js
File metadata and controls
36 lines (33 loc) · 977 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
function SelectionSortMin() {
this.name = 'Selection sort (Min)';
this.count = 40;
this.representation = 1;
this.sorter = function* (arr, colors) {
for (let i = 0; i < arr.length; ++i) {
// Find minimum element
let index = i;
passes++;
for (let j = i; j < arr.length; ++j) {
myDelay(100);
colors[j] = color('red');
colors[index] = color('blue');
yield colors;
colors[j] = color('white');
colors[index] = color('white');
compares++;
if (arr[j] < arr[index]) {
index = j;
}
}
// Swap with current element
if (index !== i) {
swap(arr, i, index);
swaps++;
}
// Current element is correctly sorted
colors[i] = color(108, 255, 51);
yield colors;
}
return colors;
}
}