Skip to content

Commit 05c50b8

Browse files
authored
Merge pull request #309 from luciankt/selection-sort-default
Make SelectionSort default to Maximum
2 parents 841e633 + 29817fd commit 05c50b8

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/algo/SelectionSort.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,18 @@ export default class SelectionSort extends Algorithm {
117117
addDivisorToAlgorithmBar();
118118

119119
const minMaxButtonList = addRadioButtonGroupToAlgorithmBar(
120-
['Minimum', 'Maximum'],
121-
'Min/Max',
120+
['Maximum', 'Minimum'],
121+
'Max/Min',
122122
);
123123

124-
this.minButton = minMaxButtonList[0];
124+
this.minButton = minMaxButtonList[1];
125125
this.minButton.onclick = this.minCallback.bind(this);
126126
this.controls.push(this.minButton);
127-
this.minButton.checked = true;
128-
this.maxButton = minMaxButtonList[1];
127+
this.maxButton = minMaxButtonList[0];
129128
this.maxButton.onclick = this.maxCallback.bind(this);
130129
this.controls.push(this.maxButton);
131-
this.isMin = true;
130+
this.maxButton.checked = true;
131+
this.isMin = false;
132132
}
133133

134134
setup() {
@@ -165,14 +165,14 @@ export default class SelectionSort extends Algorithm {
165165
this.code = [
166166
['procedure SelectionSort(array):'],
167167
[' length ← length of array'],
168-
[' for i ← 0, length do'],
169-
[' min ← i'],
170-
[' for j ← i + 1, length do'],
171-
[' if array[j] < array[min]'],
172-
[' min ← j'],
168+
[' for i ← length - 1, 0 do'],
169+
[' max ← i'],
170+
[' for j ← i - 1, 0 do'],
171+
[' if array[j] > array[max]'],
172+
[' max ← j'],
173173
[' end if'],
174174
[' end for'],
175-
[' swap array[min], array[i]'],
175+
[' swap array[max], array[i]'],
176176
[' end for'],
177177
['end procedure'],
178178
];
@@ -198,13 +198,13 @@ export default class SelectionSort extends Algorithm {
198198
this.swapCountID = this.nextIndex++;
199199
this.swapCount = 0;
200200
this.codeID = this.addCodeToCanvasBase(this.code, CODE_START_X, CODE_START_Y);
201-
if (!this.isMin) {
202-
this.cmd(act.setText, this.codeID[2][0], ' for i ← length - 1, 0 do');
203-
this.cmd(act.setText, this.codeID[3][0], ' max ← i');
204-
this.cmd(act.setText, this.codeID[4][0], ' for j ← i - 1, 0 do');
205-
this.cmd(act.setText, this.codeID[5][0], ' if array[j] > array[max]');
206-
this.cmd(act.setText, this.codeID[5][0], ' max ← j');
207-
this.cmd(act.setText, this.codeID[9][0], ' swap array[max], array[i]');
201+
if (this.isMin) {
202+
this.cmd(act.setText, this.codeID[2][0], ' for i ← 0, length do');
203+
this.cmd(act.setText, this.codeID[3][0], ' min ← i');
204+
this.cmd(act.setText, this.codeID[4][0], ' for j ← i + 1, length do');
205+
this.cmd(act.setText, this.codeID[5][0], ' if array[j] < array[min]');
206+
this.cmd(act.setText, this.codeID[5][0], ' min ← j');
207+
this.cmd(act.setText, this.codeID[9][0], ' swap array[min], array[i]');
208208
}
209209
}
210210

0 commit comments

Comments
 (0)