File tree 3 files changed +49
-0
lines changed
3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ ODD-EVEN SORT
3
+
4
+ Odd-even sort is a variant of bubble sort that alternates on elements at
5
+ odd and even indices.
6
+
7
+ If the two highlighted elements are out of order, hit LEFT ARROW to swap
8
+ them. Otherwise, hit RIGHT ARROW to continue.
9
+ """
10
+
11
+ class_name OddEvenSort
12
+ extends ComparisonSort
13
+
14
+ const ACTIONS = {
15
+ "SWAP" : "Left" ,
16
+ "CONTINUE" : "Right" ,
17
+ }
18
+ var _index = 1
19
+ var _swapped = false
20
+
21
+ func _init (array ).(array ):
22
+ pass
23
+
24
+ func next (action ):
25
+ if array .at (_index ) > array .at (_index + 1 ):
26
+ if action != null and action != ACTIONS .SWAP :
27
+ return emit_signal ("mistake" )
28
+ array .swap (_index , _index + 1 )
29
+ _swapped = true
30
+ elif action != null and action != ACTIONS .CONTINUE :
31
+ return emit_signal ("mistake" )
32
+ _index += 2
33
+ if _index + 1 >= array .size :
34
+ if _index % 2 == 0 and not _swapped :
35
+ emit_signal ("done" )
36
+ _index = 1 if _index % 2 == 0 else 0
37
+ _swapped = false
38
+
39
+ func get_effect (i ):
40
+ if i == _index or i == _index + 1 :
41
+ return EFFECTS .HIGHLIGHTED
42
+ return EFFECTS .NONE
Original file line number Diff line number Diff line change @@ -65,6 +65,11 @@ _global_script_classes=[ {
65
65
"path" : "res://levels/merge_sort.gd"
66
66
}, {
67
67
"base" : "ComparisonSort" ,
68
+ "class" : "OddEvenSort" ,
69
+ "language" : "GDScript" ,
70
+ "path" : "res://levels/odd_even_sort.gd"
71
+ }, {
72
+ "base" : "ComparisonSort" ,
68
73
"class" : "QuickSort" ,
69
74
"language" : "GDScript" ,
70
75
"path" : "res://levels/quick_sort.gd"
@@ -91,6 +96,7 @@ _global_script_class_icons={
91
96
"CycleSort" : "" ,
92
97
"InsertionSort" : "" ,
93
98
"MergeSort" : "" ,
99
+ "OddEvenSort" : "" ,
94
100
"QuickSort" : "" ,
95
101
"SelectionSort" : "" ,
96
102
"ShellSort" : ""
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ const LEVELS = [
10
10
ShellSort ,
11
11
CombSort ,
12
12
CycleSort ,
13
+ OddEvenSort ,
13
14
]
14
15
const MIN_WAIT = 1.0 / 32 # Should be greater than maximum frame time
15
16
const MAX_WAIT = 4
You can’t perform that action at this time.
0 commit comments