File tree 3 files changed +57
-0
lines changed
3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ CYCLE SORT
3
+
4
+ Cycle sort repeatedly counts the number of elements less than the first
5
+ and swaps it with that index until the smallest element is reached. Then
6
+ it does this process starting at the next out-of-place element.
7
+
8
+ If the highlighted element is less than the pointer, hit LEFT ARROW.
9
+ Otherwise, hit RIGHT ARROW.
10
+ """
11
+
12
+ class_name CycleSort
13
+ extends ComparisonSort
14
+
15
+ const ACTIONS = {
16
+ "SMALLER" : "Left" ,
17
+ "BIGGER" : "Right" ,
18
+ }
19
+ var _pointer = 0
20
+ var _index = 0
21
+ var _smaller = 0
22
+
23
+ func _init (array ).(array ):
24
+ pass
25
+
26
+ func next (action ):
27
+ if array .at (_index ) < array .at (_pointer ):
28
+ if action != null and action != ACTIONS .SMALLER :
29
+ return emit_signal ("mistake" )
30
+ _smaller += 1
31
+ elif array .at (_index ) >= array .at (_pointer ):
32
+ if action != null and action != ACTIONS .BIGGER :
33
+ return emit_signal ("mistake" )
34
+ _index += 1
35
+ if _index == array .size :
36
+ array .swap (_pointer , _smaller )
37
+ while array .at (_pointer ) == _pointer + 1 :
38
+ _pointer += 1
39
+ if _pointer == array .size :
40
+ return emit_signal ("done" )
41
+ _index = 0
42
+ _smaller = 0
43
+
44
+ func get_effect (i ):
45
+ if i == _index :
46
+ return EFFECTS .HIGHLIGHTED
47
+ return EFFECTS .NONE
48
+
49
+ func get_pointer ():
50
+ return _pointer
Original file line number Diff line number Diff line change @@ -50,6 +50,11 @@ _global_script_classes=[ {
50
50
"path" : "res://levels/comparison_sort.gd"
51
51
}, {
52
52
"base" : "ComparisonSort" ,
53
+ "class" : "CycleSort" ,
54
+ "language" : "GDScript" ,
55
+ "path" : "res://levels/cycle_sort.gd"
56
+ }, {
57
+ "base" : "ComparisonSort" ,
53
58
"class" : "InsertionSort" ,
54
59
"language" : "GDScript" ,
55
60
"path" : "res://levels/insertion_sort.gd"
@@ -83,6 +88,7 @@ _global_script_class_icons={
83
88
"CocktailSort" : "" ,
84
89
"CombSort" : "" ,
85
90
"ComparisonSort" : "" ,
91
+ "CycleSort" : "" ,
86
92
"InsertionSort" : "" ,
87
93
"MergeSort" : "" ,
88
94
"QuickSort" : "" ,
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ const LEVELS = [
9
9
CocktailSort ,
10
10
ShellSort ,
11
11
CombSort ,
12
+ CycleSort ,
12
13
]
13
14
const MIN_WAIT = 1.0 / 32 # Should be greater than maximum frame time
14
15
const MAX_WAIT = 4
You can’t perform that action at this time.
0 commit comments