File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Expand file tree Collapse file tree 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=[ {
5050"path" : "res://levels/comparison_sort.gd"
5151}, {
5252"base" : "ComparisonSort" ,
53+ "class" : "CycleSort" ,
54+ "language" : "GDScript" ,
55+ "path" : "res://levels/cycle_sort.gd"
56+ }, {
57+ "base" : "ComparisonSort" ,
5358"class" : "InsertionSort" ,
5459"language" : "GDScript" ,
5560"path" : "res://levels/insertion_sort.gd"
@@ -83,6 +88,7 @@ _global_script_class_icons={
8388"CocktailSort" : "" ,
8489"CombSort" : "" ,
8590"ComparisonSort" : "" ,
91+ "CycleSort" : "" ,
8692"InsertionSort" : "" ,
8793"MergeSort" : "" ,
8894"QuickSort" : "" ,
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ const LEVELS = [
99 CocktailSort ,
1010 ShellSort ,
1111 CombSort ,
12+ CycleSort ,
1213]
1314const MIN_WAIT = 1.0 / 32 # Should be greater than maximum frame time
1415const MAX_WAIT = 4
You can’t perform that action at this time.
0 commit comments