File tree Expand file tree Collapse file tree 13 files changed +69
-65
lines changed Expand file tree Collapse file tree 13 files changed +69
-65
lines changed Original file line number Diff line number Diff line change 1
- class_name BogoSort
2
- extends ComparisonSort
1
+ """
2
+ BOGOSORT
3
3
4
- const NAME = "BOGOSORT"
5
- const ABOUT = """
6
4
Generates random permutations until the array is sorted.
7
- """
8
- const CONTROLS = """
5
+
9
6
Keep on hitting RIGHT ARROW to CONTINUE and hope for the best!
10
7
"""
11
8
9
+ class_name BogoSort
10
+ extends ComparisonSort
11
+
12
12
func _init (array ).(array ):
13
13
pass
14
14
Original file line number Diff line number Diff line change 1
- class_name BubbleSort
2
- extends ComparisonSort
1
+ """
2
+ BUBBLE SORT
3
3
4
- const NAME = "BUBBLE SORT"
5
- const ABOUT = """
6
4
Bubble sort iterates through the array and looks at each pair of
7
5
elements, swapping them if they are out of order. When it has gone
8
6
through the entire array without swapping a single pair, it has
9
7
finished. Though simple to understand, bubble sort is hopelessly
10
8
inefficient on all but the smallest of arrays.
11
- """
12
- const CONTROLS = """
9
+
13
10
If the two highlighted elements are out of order, hit LEFT ARROW to swap
14
11
them. Otherwise, hit RIGHT ARROW to continue.
15
12
"""
16
13
14
+ class_name BubbleSort
15
+ extends ComparisonSort
16
+
17
17
const ACTIONS = {
18
18
"SWAP" : "Left" ,
19
19
"CONTINUE" : "Right" ,
Original file line number Diff line number Diff line change 1
- class_name CocktailSort
2
- extends ComparisonSort
1
+ """
2
+ COCKTAIL SORT
3
3
4
- const NAME = "COCKTAIL SORT"
5
- const ABOUT = """
6
4
Cocktail shaker sort is a variation of bubble sort that
7
5
alternates going backwards and forwards.
8
- """
9
- const CONTROLS = """
6
+
10
7
If the two highlighted elements are out of order, hit LEFT ARROW to swap
11
8
them. Otherwise, hit RIGHT ARROW to continue.
12
9
"""
13
10
11
+ class_name CocktailSort
12
+ extends ComparisonSort
13
+
14
14
const ACTIONS = {
15
15
"SWAP" : "Left" ,
16
16
"CONTINUE" : "Right" ,
Original file line number Diff line number Diff line change 1
- class_name CombSort
2
- extends ComparisonSort
1
+ """
2
+ COMB SORT
3
3
4
- const NAME = "COMB SORT"
5
- const ABOUT = """
6
4
Comb sort is a variant of bubble sort that operates on gapped arrays.
7
- """
8
- const CONTROLS = """
5
+
9
6
If the two highlighted elements are out of order, hit LEFT ARROW to swap
10
7
them. Otherwise, hit RIGHT ARROW to continue.
11
8
"""
12
9
10
+ class_name CombSort
11
+ extends ComparisonSort
12
+
13
13
const SHRINK_FACTOR = 1.3
14
14
const ACTIONS = {
15
15
"SWAP" : "Left" ,
Original file line number Diff line number Diff line change @@ -11,9 +11,13 @@ const EFFECTS = {
11
11
}
12
12
13
13
const DISABLE_TIME = 1.0
14
+ var NAME = _get_header ().split (" " )[0 ]
15
+ var DESCRIPTION = _get_header ().split (" " )[1 ]
16
+ var CONTROLS = _get_header ().split (" " )[2 ]
14
17
15
18
var array : ArrayModel
16
19
var moves = 0
20
+ var test = _get_header ().split (" " )[0 ]
17
21
18
22
var _timer = Timer .new ()
19
23
@@ -26,6 +30,9 @@ func _init(array):
26
30
self .connect ("mistake" , self , "_on_ComparisonSort_mistake" )
27
31
self .connect ("done" , self , "_on_ComparisonSort_done" )
28
32
33
+ func _get_header ():
34
+ return get_script ().source_code .replace ("\n " , " " ).split ('"""' )[1 ].strip_edges ()
35
+
29
36
func _ready ():
30
37
set_process_input (false )
31
38
Original file line number Diff line number Diff line change 1
- class_name InsertionSort
2
- extends ComparisonSort
1
+ """
2
+ INSERTION SORT
3
3
4
- const NAME = "INSERTION SORT"
5
- const ABOUT = """
6
4
Insertion sort goes through the array and inserts each
7
5
element into its correct position. It is most similar to how most people
8
6
would sort a deck of cards. It is also slow on large arrays but it is
9
7
one of the faster quadratic algorithms. It is often used to sort smaller
10
8
subarrays in hybrid sorting algorithms.
11
- """
12
- const CONTROLS = """
9
+
13
10
Hit LEFT ARROW to swap the two highlighted elements as long as they are
14
11
out of order. When this is no longer the case, hit RIGHT ARROW to
15
12
advance.
16
13
"""
17
14
15
+ class_name InsertionSort
16
+ extends ComparisonSort
17
+
18
18
const ACTIONS = {
19
19
"SWAP" : "Left" ,
20
20
"CONTINUE" : "Right" ,
Original file line number Diff line number Diff line change 1
- class_name MergeSort
2
- extends ComparisonSort
1
+ """
2
+ MERGE SORT
3
3
4
- const NAME = "MERGE SORT"
5
- const ABOUT = """
6
4
Merge sort is an efficient sorting algorithm that splits the array into
7
5
single-element chunks. Then it merges each pair of chunks until only one
8
6
sorted chunk is left by repeatedly choosing the smaller element at the
9
7
head of each chunk and moving the head back. However, it needs an entire
10
8
array's worth of auxiliary memory.
11
- """
12
- const CONTROLS = """
9
+
13
10
Press the ARROW KEY corresponding to the side that the smaller
14
11
highlighted element is on. If you've reached the end of one side, press
15
12
the other side's ARROW KEY.
16
13
"""
17
14
15
+ class_name MergeSort
16
+ extends ComparisonSort
17
+
18
18
const ACTIONS = {
19
19
"LEFT" : "Left" ,
20
20
"RIGHT" : "Right" ,
Original file line number Diff line number Diff line change 1
- class_name QuickSort
2
- extends ComparisonSort
1
+ """
2
+ QUICKSORT
3
3
4
- const NAME = "QUICKSORT"
5
- const ABOUT = """
6
4
Quicksort designates the last element as the pivot and puts everything
7
5
less than the pivot before it and everything greater after it. This
8
6
partitioning is done by iterating through the array while keeping track
9
7
of a pointer initially set to the first element. Every time an element
10
8
less than the pivot is encountered, it is swapped with the pointed
11
9
element and the pointer moves forward. At the end, the pointer and pivot
12
10
are swapped, and the process is repeated on the left and right halves.
13
- """
14
- const CONTROLS = """
11
+
15
12
If the highlighted element is less than the pivot or the pivot has been
16
13
reached, press LEFT ARROW to swap it with the pointer. Otherwise, press
17
14
RIGHT ARROW to move on.
18
15
"""
19
16
17
+ class_name QuickSort
18
+ extends ComparisonSort
19
+
20
20
const ACTIONS = {
21
21
"SWAP" : "Left" ,
22
22
"CONTINUE" : "Right" ,
Original file line number Diff line number Diff line change 1
- class_name SelectionSort
2
- extends ComparisonSort
1
+ """
2
+ SELECTION SORT
3
3
4
- const NAME = "SELECTION SORT"
5
- const ABOUT = """
6
4
Selection sort incrementally builds a sorted array by repeatedly looking
7
5
for the smallest element and swapping it onto the end of the sorted
8
6
portion of the array, which initially starts with size zero but grows
9
7
after each round. It is faster than an unoptimized bubble sort but
10
8
slower than insertion sort.
11
- """
12
- const CONTROLS = """
9
+
13
10
Keep on hitting RIGHT ARROW until you encounter an element that is
14
11
smaller than the left highlighted element, then hit LEFT ARROW and
15
12
repeat.
16
13
"""
17
14
15
+
16
+ class_name SelectionSort
17
+ extends ComparisonSort
18
+
18
19
const ACTIONS = {
19
20
"SWAP" : "Left" ,
20
21
"CONTINUE" : "Right" ,
Original file line number Diff line number Diff line change 1
- class_name ShellSort
2
- extends ComparisonSort
1
+ """
2
+ SHELL SORT
3
3
4
- const NAME = "SHELL SORT"
5
- const ABOUT = """
6
4
Shell sort is a variation of insertion sort that sorts arrays separated
7
5
by gaps.
8
- """
9
- const CONTROLS = """
6
+
10
7
Hit LEFT ARROW to swap the two highlighted elements as long as they are
11
8
out of order. When this is no longer the case, hit RIGHT ARROW to
12
- advance.
13
9
"""
14
10
11
+ class_name ShellSort
12
+ extends ComparisonSort
13
+
15
14
const ACTIONS = {
16
15
"SWAP" : "Left" ,
17
16
"CONTINUE" : "Right" ,
You can’t perform that action at this time.
0 commit comments