Skip to content

Commit 907db7c

Browse files
committed
feat: add shell sort
To make it easier to focus on the current gap subarray in shell sort, dimmed elements are now even darker (opacity decreased 4x from 0.5 -> 0.125).
1 parent 780bb50 commit 907db7c

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

levels/shell_sort.gd

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class_name ShellSort
2+
extends ComparisonSort
3+
4+
const NAME = "SHELL SORT"
5+
const ABOUT = """
6+
Shell sort is a variation of insertion sort that sorts arrays separated
7+
by gaps.
8+
"""
9+
const CONTROLS = """
10+
Hit LEFT ARROW to swap the two highlighted elements as long as they are
11+
out of order. When this is no longer the case, hit RIGHT ARROW to
12+
advance.
13+
"""
14+
15+
const ACTIONS = {
16+
"SWAP": "Left",
17+
"CONTINUE": "Right",
18+
}
19+
var _gap = array.size / 2
20+
var _begin = 0 # First element of subarray
21+
var _end = _gap # End of the sorted subarray
22+
var _index = _gap # Position of element currently being inserted
23+
24+
func _init(array).(array):
25+
pass
26+
27+
func next(action):
28+
if array.at(_index - _gap) > array.at(_index):
29+
if action != null and action != ACTIONS.SWAP:
30+
return emit_signal("mistake")
31+
array.swap(_index - _gap, _index)
32+
_index -= _gap
33+
if _index - _gap < 0:
34+
_grow()
35+
else:
36+
if action != null and action != ACTIONS.CONTINUE:
37+
return emit_signal("mistake")
38+
_grow()
39+
40+
func get_effect(i):
41+
if i == _index - _gap or i == _index:
42+
return EFFECTS.HIGHLIGHTED
43+
if (i - _begin) % _gap != 0:
44+
return EFFECTS.DIMMED
45+
return EFFECTS.NONE
46+
47+
func get_pointer():
48+
return _end
49+
50+
func _grow():
51+
_end += _gap
52+
_index = _end
53+
if _end >= array.size:
54+
_begin += 1
55+
if _begin >= _gap:
56+
_gap /= 2
57+
if _gap == 0:
58+
emit_signal("done")
59+
_begin = 0
60+
_end = _gap + _begin
61+
_index = _gap + _begin

project.godot

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ _global_script_classes=[ {
6868
"class": "SelectionSort",
6969
"language": "GDScript",
7070
"path": "res://levels/selection_sort.gd"
71+
}, {
72+
"base": "ComparisonSort",
73+
"class": "ShellSort",
74+
"language": "GDScript",
75+
"path": "res://levels/shell_sort.gd"
7176
} ]
7277
_global_script_class_icons={
7378
"ArrayModel": "",
@@ -81,7 +86,8 @@ _global_script_class_icons={
8186
"MergeSort": "",
8287
"QuickSort": "",
8388
"Score": "",
84-
"SelectionSort": ""
89+
"SelectionSort": "",
90+
"ShellSort": ""
8591
}
8692

8793
[application]

scripts/levels.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const LEVELS = [
77
MergeSort,
88
QuickSort,
99
CocktailSort,
10+
ShellSort,
1011
]
1112
const MIN_WAIT = 1.0 / 32 # Should be greater than maximum frame time
1213
const MAX_WAIT = 4

scripts/theme.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Global constants relating to the GUI.
55
extends Node
66

77
const GREEN = Color("33ff33")
8-
const DARK_GREEN = Color("1b5e20")
8+
const DARK_GREEN = Color("072107")
99
const ORANGE = Color("ffb000")
1010
const RED = Color("f44336")
1111
const BLUE = Color("2196f3")

views/array_view.gd

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,14 @@ func _on_ArrayModel_swapped(i, j):
8989
_tween.start()
9090

9191
func _draw():
92-
var width = rect_size.x + MARGIN
93-
var height = rect_size.y + MARGIN
92+
# Prevent lines from "sticking" into border
93+
var margin = preload("res://scripts/border.gd").WIDTH / 2
94+
var width = rect_size.x + MARGIN - margin
95+
var height = rect_size.y + MARGIN - margin
96+
var start = -MARGIN + margin
97+
# Vertical lines
9498
for i in range(-MARGIN + BOX_SIZE, width, BOX_SIZE):
95-
draw_line(Vector2(i, -MARGIN), Vector2(i, height), LINE_COLOR)
99+
draw_line(Vector2(i, start), Vector2(i, height - 1), LINE_COLOR)
100+
# Horizontal lines
96101
for i in range(-MARGIN + BOX_SIZE, height, BOX_SIZE):
97-
draw_line(Vector2(-MARGIN, i), Vector2(width, i), LINE_COLOR)
102+
draw_line(Vector2(start, i), Vector2(width - 1, i), LINE_COLOR)

0 commit comments

Comments
 (0)