Skip to content

Commit 741dfe2

Browse files
committed
feat: add selection sort
A modified version of selection sort is now available to play. To save development time, instead of just keeping track of the smallest element, it actually swaps every time it encounters a new low. This will likely have to be changed in a future release as it's pretty confusing.
1 parent 3c393fd commit 741dfe2

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

levels/selection_sort.gd

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
extends ComparisonSort
2+
class_name SelectionSort
3+
4+
const TITLE = "SELECTION SORT"
5+
const ABOUT = """Selection sort incrementally builds a sorted array by
6+
repeatedly looking for the smallest element and swapping it onto the
7+
end of the sorted portion of the array, which initially starts with size
8+
zero but grows after each round. It is faster than an unoptimized bubble
9+
sort but slower than insertion sort."""
10+
var base = 0
11+
var index = base + 1
12+
13+
func _init(array).(array):
14+
pass
15+
16+
func check(action):
17+
if array.get(base) > array.get(index):
18+
return action == "swap"
19+
else:
20+
return action == "no_swap"
21+
22+
func next():
23+
if array.get(base) > array.get(index):
24+
array.swap(base, index)
25+
index += 1
26+
if index == array.size:
27+
base += 1
28+
index = base + 1
29+
if base == array.size - 1:
30+
emit_signal("done")
31+
32+
func emphasized(i):
33+
return i == index or i == base

project.godot

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,20 @@ _global_script_classes=[ {
3838
"class": "InsertionSort",
3939
"language": "GDScript",
4040
"path": "res://levels/insertion_sort.gd"
41+
}, {
42+
"base": "ComparisonSort",
43+
"class": "SelectionSort",
44+
"language": "GDScript",
45+
"path": "res://levels/selection_sort.gd"
4146
} ]
4247
_global_script_class_icons={
4348
"ArrayModel": "",
4449
"ArrayView": "",
4550
"BogoSort": "",
4651
"BubbleSort": "",
4752
"ComparisonSort": "",
48-
"InsertionSort": ""
53+
"InsertionSort": "",
54+
"SelectionSort": ""
4955
}
5056

5157
[application]

scripts/levels.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extends HBoxContainer
33
var levels = [
44
BubbleSort,
55
InsertionSort,
6+
SelectionSort,
67
]
78
var level: ComparisonSort
89

0 commit comments

Comments
 (0)