-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselectionMax.js
More file actions
37 lines (33 loc) · 976 Bytes
/
selectionMax.js
File metadata and controls
37 lines (33 loc) · 976 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
function SelectionSortMax() {
this.name = 'Selection sort (Max)';
this.count = 40;
this.representation = 1;
this.sorter = function* (arr, colors) {
for (let i = arr.length - 1; i >= 0; --i) {
// Find maximum element
let index = i;
passes++;
for (let j = 0; j <= i; ++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(128, 255, 51);
yield colors;
}
return colors;
}
}