Skip to content

Commit ea09395

Browse files
committed
feat: add cocktail shaker sort
Abbreviated as cocktail sort for now to save screen space.
1 parent e8712bc commit ea09395

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

levels/cocktail_sort.gd

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class_name CocktailSort
2+
extends ComparisonSort
3+
4+
const NAME = "COCKTAIL SORT"
5+
const ABOUT = """
6+
Cocktail shaker sort is a variation of bubble sort that
7+
alternates going backwards and forwards.
8+
"""
9+
const CONTROLS = """
10+
If the two highlighted elements are out of order, hit LEFT ARROW to swap
11+
them. Otherwise, hit RIGHT ARROW to continue.
12+
"""
13+
14+
const ACTIONS = {
15+
"SWAP": "Left",
16+
"CONTINUE": "Right",
17+
}
18+
var _index = 0 # First of two elements being compared
19+
var _sorted = 0 # Size of the sorted subarray at the end of the array
20+
var _forwards = true
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 + 1):
28+
if action != null and action != ACTIONS.SWAP:
29+
return emit_signal("mistake")
30+
array.swap(_index, _index + 1)
31+
_swapped = true
32+
else:
33+
if action != null and action != ACTIONS.CONTINUE:
34+
return emit_signal("mistake")
35+
if _forwards:
36+
_index += 1
37+
if _index == array.size - _sorted - 1:
38+
if not _swapped:
39+
emit_signal("done")
40+
_swapped = false
41+
_forwards = false
42+
_index -= 2
43+
_sorted += 1
44+
else:
45+
_index -= 1
46+
if _index == _sorted - 2:
47+
if not _swapped:
48+
emit_signal("done")
49+
_swapped = false
50+
_forwards = true
51+
_index += 2
52+
53+
func get_effect(i):
54+
if i == _index or i == _index + 1:
55+
return EFFECTS.HIGHLIGHTED
56+
if i < _sorted and _forwards == true or i < _sorted - 1 or i >= array.size - _sorted:
57+
return EFFECTS.DIMMED
58+
return EFFECTS.NONE

levels/quick_sort.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const ACTIONS = {
2121
"SWAP": "Left",
2222
"CONTINUE": "Right",
2323
}
24-
var _index = 0
25-
var _pointer = 0
24+
var _index = 0 # Index of element being compared with pivot
25+
var _pointer = 0 # Boundary between partitions
2626
# Bookkeep simulated recursion with a binary tree of subarray bounds
2727
var _stack = BinaryTreeModel.new(Vector2(0, array.size - 1))
2828

project.godot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ _global_script_classes=[ {
3434
"language": "GDScript",
3535
"path": "res://levels/bubble_sort.gd"
3636
}, {
37+
"base": "ComparisonSort",
38+
"class": "CocktailSort",
39+
"language": "GDScript",
40+
"path": "res://levels/cocktail_sort.gd"
41+
}, {
3742
"base": "Node",
3843
"class": "ComparisonSort",
3944
"language": "GDScript",
@@ -70,6 +75,7 @@ _global_script_class_icons={
7075
"BinaryTreeModel": "",
7176
"BogoSort": "",
7277
"BubbleSort": "",
78+
"CocktailSort": "",
7379
"ComparisonSort": "",
7480
"InsertionSort": "",
7581
"MergeSort": "",

scripts/levels.gd

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

0 commit comments

Comments
 (0)