File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ class_name CombSort
2
+ extends ComparisonSort
3
+
4
+ const NAME = "COMB SORT"
5
+ const ABOUT = """
6
+ Comb sort is a variant of bubble sort that operates on gapped arrays.
7
+ """
8
+ const CONTROLS = """
9
+ If the two highlighted elements are out of order, hit LEFT ARROW to swap
10
+ them. Otherwise, hit RIGHT ARROW to continue.
11
+ """
12
+
13
+ const SHRINK_FACTOR = 1.3
14
+ const ACTIONS = {
15
+ "SWAP" : "Left" ,
16
+ "CONTINUE" : "Right" ,
17
+ }
18
+ var _gap : int = array .size / SHRINK_FACTOR
19
+ var _index = 0 # First of two elements being compared
20
+ var _end = array .size # Beginning of sorted subarray
21
+ var _swapped = false
22
+
23
+ func _init (array ).(array ):
24
+ pass
25
+
26
+ func next (action ):
27
+ if array .at (_index ) > array .at (_index + _gap ):
28
+ if action != null and action != ACTIONS .SWAP :
29
+ return emit_signal ("mistake" )
30
+ array .swap (_index , _index + _gap )
31
+ _swapped = true
32
+ elif action != null and action != ACTIONS .CONTINUE :
33
+ return emit_signal ("mistake" )
34
+ _index += 1
35
+ if _index + _gap >= array .size :
36
+ if not _swapped and _gap == 1 :
37
+ emit_signal ("done" )
38
+ _gap /= SHRINK_FACTOR
39
+ _gap = max (_gap , 1 )
40
+ _index = 0
41
+ _swapped = false
42
+
43
+ func get_effect (i ):
44
+ if i == _index or i == _index + _gap :
45
+ return EFFECTS .HIGHLIGHTED
46
+ if i >= _end :
47
+ return EFFECTS .DIMMED
48
+ return EFFECTS .NONE
Original file line number Diff line number Diff line change @@ -39,6 +39,11 @@ _global_script_classes=[ {
39
39
"language" : "GDScript" ,
40
40
"path" : "res://levels/cocktail_sort.gd"
41
41
}, {
42
+ "base" : "ComparisonSort" ,
43
+ "class" : "CombSort" ,
44
+ "language" : "GDScript" ,
45
+ "path" : "res://levels/comb_sort.gd"
46
+ }, {
42
47
"base" : "Node" ,
43
48
"class" : "ComparisonSort" ,
44
49
"language" : "GDScript" ,
@@ -81,6 +86,7 @@ _global_script_class_icons={
81
86
"BogoSort" : "" ,
82
87
"BubbleSort" : "" ,
83
88
"CocktailSort" : "" ,
89
+ "CombSort" : "" ,
84
90
"ComparisonSort" : "" ,
85
91
"InsertionSort" : "" ,
86
92
"MergeSort" : "" ,
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ const LEVELS = [
8
8
QuickSort ,
9
9
CocktailSort ,
10
10
ShellSort ,
11
+ CombSort ,
11
12
]
12
13
const MIN_WAIT = 1.0 / 32 # Should be greater than maximum frame time
13
14
const MAX_WAIT = 4
You can’t perform that action at this time.
0 commit comments