Skip to content

Commit a75accb

Browse files
committed
feat: change selection sort to be more realistic
Selection sort now reflects a real-world implementation, which is probably less confusing for those already familiar with it without being more difficult for newcomers.
1 parent f82f003 commit a75accb

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

levels/selection_sort.gd

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,31 @@ repeat.
1616
"""
1717

1818
var _base = 0 # Size of sorted subarray
19-
var _index = 1 # Index of tentative new smallest
19+
var _min = 0 # Index of smallest known element
20+
var _index = 1 # Element currently being compared
2021

2122
func _init(array).(array):
2223
pass
2324

2425
func next(action):
25-
if array.at(_base) > array.at(_index):
26+
if array.at(_index) < array.at(_min):
2627
if action != null and action != ACTIONS.SWAP:
2728
return emit_signal("mistake")
28-
array.swap(_base, _index)
29+
_min = _index
2930
elif action != null and action != ACTIONS.NO_SWAP:
3031
return emit_signal("mistake")
3132
_index += 1
3233
if _index == array.size:
34+
array.swap(_base, _min)
3335
_base += 1
36+
_min = _base
3437
_index = _base + 1
3538
if _base == array.size - 1:
3639
emit_signal("done")
3740

3841
func get_effect(i):
39-
if i == _index or i == _base:
42+
if i == _min or i == _index:
4043
return EFFECTS.HIGHLIGHTED
41-
if i <= _base:
44+
if i < _base:
4245
return EFFECTS.DIMMED
4346
return EFFECTS.NONE

0 commit comments

Comments
 (0)