Skip to content

Commit 8ebc0f7

Browse files
committed
feat: dim already compared elements in merge sort
Previously, all elements in the left and right subarrays would be highlighted. Now, those elements that have already been passed over will be dimmed.
1 parent a75accb commit 8ebc0f7

File tree

3 files changed

+10
-21
lines changed

3 files changed

+10
-21
lines changed

levels/merge_sort.gd

+10-14
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ func _init(array).(array):
2424
pass
2525

2626
func next(action):
27-
if _left == -1:
27+
if _left == _get_middle():
2828
if action != null and action != ACTIONS.RIGHT:
2929
return emit_signal("mistake")
3030
_right += 1
31-
elif _right == -1:
31+
elif _right == _get_end():
3232
if action != null and action != ACTIONS.LEFT:
3333
return emit_signal("mistake")
3434
_left += 1
@@ -40,30 +40,26 @@ func next(action):
4040
if action != null and action != ACTIONS.RIGHT:
4141
return emit_signal("mistake")
4242
_right += 1
43-
# Test if end of subarrays have been reached
44-
if _left == _get_middle():
45-
_left = -1
46-
if _right == _get_end():
47-
_right = -1
4843
# If both ends have been reached, merge and advance to next block
49-
if _left == -1 and _right == -1:
44+
if _left == _get_middle() and _right == _get_end():
5045
array.sort(_get_begin(), _get_end())
5146
_sub_no += 1
52-
_left = _get_begin()
53-
_right = _get_middle()
5447
# If last block has been completed, go up a level
5548
if _sub_no == array.size / (_sub_size):
5649
_sub_size *= 2
5750
_sub_no = 0
58-
_left = _get_begin()
59-
_right = _get_middle()
6051
if _sub_size == array.size * 2:
6152
emit_signal("done")
53+
# Update pointers
54+
_left = _get_begin()
55+
_right = _get_middle()
6256

6357
func get_effect(i):
64-
if i == _left or i == _right:
58+
var is_left = _left != _get_middle() and i == _left
59+
var is_right = _right != _get_end() and i == _right
60+
if is_left or is_right:
6561
return EFFECTS.HIGHLIGHTED
66-
if i < _sub_no * _sub_size or i >= _sub_no * _sub_size + _sub_size:
62+
if i < _left or i >= _get_middle() and i < _right or i >= _get_end():
6763
return EFFECTS.DIMMED
6864
return EFFECTS.NONE
6965

project.godot

-6
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ _global_script_classes=[ {
4545
"path": "res://levels/merge_sort.gd"
4646
}, {
4747
"base": "ComparisonSort",
48-
"class": "QuickSort",
49-
"language": "GDScript",
50-
"path": "res://levels/quick_sort.gd"
51-
}, {
52-
"base": "ComparisonSort",
5348
"class": "SelectionSort",
5449
"language": "GDScript",
5550
"path": "res://levels/selection_sort.gd"
@@ -62,7 +57,6 @@ _global_script_class_icons={
6257
"ComparisonSort": "",
6358
"InsertionSort": "",
6459
"MergeSort": "",
65-
"QuickSort": "",
6660
"SelectionSort": ""
6761
}
6862

scripts/levels.gd

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const LEVELS = [
55
InsertionSort,
66
SelectionSort,
77
MergeSort,
8-
QuickSort,
98
]
109
var _level: ComparisonSort
1110

0 commit comments

Comments
 (0)