File tree 12 files changed +92
-57
lines changed
12 files changed +92
-57
lines changed Original file line number Diff line number Diff line change 2
2
BUBBLE SORT
3
3
4
4
5
- Bubble sort iterates through the array and looks at each pair of
6
- elements, swapping them if they are out of order. When it has gone
7
- through the entire array without swapping a single pair, it has
8
- finished. Though simple to understand, bubble sort is hopelessly
9
- inefficient on all but the smallest of arrays.
5
+ Bubble sort looks at consecutive pairs of elements and swaps them if
6
+ they are out of order, finishing when it has gone through the whole
7
+ array from beginning to end without a single swap. The actual level
8
+ contains an optimization that skips over elements guaranteed to be
9
+ already in place.
10
+
11
+ Due to its simplicity, it is commonly taught as the first sorting
12
+ algorithm students learn in computer science classes, but is rarely used
13
+ in real life because it is slow on large data and other simple quadratic
14
+ algorithms like insertion sort perform better.
10
15
11
16
12
17
If the two highlighted elements are out of order, hit LEFT ARROW to swap
Original file line number Diff line number Diff line change 2
2
COCKTAIL SORT
3
3
4
4
5
- Cocktail shaker sort is a variation of bubble sort that
6
- alternates going backwards and forwards.
5
+ Cocktail sort is a variation of bubble sort that alternates going
6
+ backwards and forwards. The actual level contains an optimization that
7
+ skips over elements guaranteed to be already in place.
8
+
9
+ Because it is bidirectional, it is slightly faster than bubble sort, but
10
+ is still quadratic and therefore not used on large data.
7
11
8
12
9
13
If the two highlighted elements are out of order, hit LEFT ARROW to swap
@@ -22,10 +26,8 @@ def cocktail_sort(a):
22
26
if array[i] > array[i + 1]:
23
27
a.swap(i, i + 1)
24
28
swapped = true
25
-
26
29
if not swapped:
27
30
break
28
-
29
31
swapped = false
30
32
for i in range(len(a) - 1, 0, -1)
31
33
if a[i - 1] > a[i]:
Original file line number Diff line number Diff line change 2
2
COMB SORT
3
3
4
4
5
- Comb sort is a variant of bubble sort that operates on gapped arrays.
5
+ Comb sort is a variant of bubble sort that compares elements a certain
6
+ gap apart instead of consecutive elements. This gap is divided after
7
+ every pass by an experimentally determined optimal factor of about 1.3.
8
+ Once the gap becomes 1, comb sort becomes a regular bubble sort.
9
+
10
+ This allows comb sort to get rid of small values near the end more
11
+ quickly, which turns out to be the bottleneck in bubble sort, but still
12
+ has a quadratic worst case.
6
13
7
14
8
15
If the two highlighted elements are out of order, hit LEFT ARROW to swap
@@ -17,6 +24,7 @@ def comb_sort(a):
17
24
gap = len(a)
18
25
swapped = true
19
26
while gap != 1 or swapped:
27
+ swapped = false
20
28
gap = max(gap / 1.3, 1)
21
29
for i in range(len(a) - gap):
22
30
if a[i] > a[i + gap]:
Original file line number Diff line number Diff line change @@ -12,8 +12,8 @@ const EFFECTS = {
12
12
13
13
const DISABLE_TIME = 1.0
14
14
var NAME = _get_header ().split (" " )[0 ]
15
- var DESCRIPTION = _get_header ().split (" " )[1 ]
16
- var CONTROLS = _get_header ().split (" " )[2 ]
15
+ var DESCRIPTION = _get_header ().split (" " )[1 ]. replace ( " " , " \n\n " )
16
+ var CONTROLS = _get_header ().split (" " )[- 1 ]
17
17
18
18
var array : ArrayModel
19
19
Original file line number Diff line number Diff line change 2
2
CYCLE SORT
3
3
4
4
5
- Cycle sort repeatedly counts the number of elements less than the first
6
- and swaps it with that index until the smallest element is reached. Then
7
- it does this process starting at the next out-of-place element.
5
+ Cycle sort looks at the first element and finds its correct final
6
+ position by counting the number of elements smaller than it. Then it
7
+ saves the element at that index, writes the first element there, and
8
+ repeats the process with the saved element. For the sake of
9
+ demonstration, in the actual level, swaps are used instead.
8
10
11
+ This results in a quadratic runtime but gives it the special property
12
+ of being optimal in the number of writes to the array. This makes cycle
13
+ sort useful in storage types where writes are very expensive or reduce
14
+ its lifespan.
9
15
10
- If the highlighted element is less than the pointer, hit LEFT ARROW.
11
- Otherwise, hit RIGHT ARROW.
16
+
17
+ If the highlighted element is less than the element below the blue
18
+ pointer, hit LEFT ARROW. Otherwise, hit RIGHT ARROW.
12
19
"""
13
20
14
21
class_name CycleSort
Original file line number Diff line number Diff line change 2
2
INSERTION SORT
3
3
4
4
5
- Insertion sort goes through the array and inserts each
6
- element into its correct position. It is most similar to how most people
7
- would sort a deck of cards. It is also slow on large arrays but it is
8
- one of the faster quadratic algorithms. It is often used to sort smaller
9
- subarrays in hybrid sorting algorithms.
5
+ Insertion sort goes through the array and inserts each element into its
6
+ correct place, like how most people would sort a hand of playing cards.
10
7
8
+ It is one of the fastest quadratic algorithms in practice and is
9
+ efficient on small or almost sorted data. It is also simple, stable, and
10
+ in-place. For these reasons it is sometimes used within faster divide
11
+ and conquer algorithms when the array has been divided to a small size.
11
12
12
- Hit LEFT ARROW to swap the two highlighted elements as long as they are
13
- out of order. When this is no longer the case , hit RIGHT ARROW to
14
- advance .
13
+
14
+ If the two highlighted elements are out of order , hit LEFT ARROW to swap
15
+ them. Otherwise, hit RIGHT ARROW to continue .
15
16
"""
16
17
17
18
class_name InsertionSort
Original file line number Diff line number Diff line change 2
2
MERGE SORT
3
3
4
4
5
- Merge sort is an efficient sorting algorithm that splits the array into
6
- single-element chunks . Then it merges each pair of chunks until only one
7
- sorted chunk is left by repeatedly choosing the smaller element at the
8
- head of each chunk and moving the head back. However, it needs an entire
9
- array's worth of auxiliary memory .
5
+ Merge sort merges subarrays of increasing size by setting a pointer to
6
+ the head of each half . Then it repeatedly copies the smaller pointed
7
+ element and increments that side's pointer. When one side is exhausted,
8
+ it copies the rest of the other side and overwrites the two halves with
9
+ the merged copy .
10
10
11
11
12
12
Press the ARROW KEY corresponding to the side that the smaller
13
- highlighted element is on. If you've reached the end of one side, press
14
- the other side's ARROW KEY.
13
+ highlighted element is on or the non-exhausted side.
15
14
"""
16
15
17
16
class_name MergeSort
@@ -34,6 +33,7 @@ def merge_sort(a):
34
33
merged.append(a[i])
35
34
i += 1
36
35
a[begin:begin + size] = merged
36
+ size *= 2
37
37
"""
38
38
const ACTIONS = {
39
39
"LEFT" : "Left" ,
Original file line number Diff line number Diff line change 2
2
ODD-EVEN SORT
3
3
4
4
5
- Odd-even sort is a variant of bubble sort that alternates on elements at
6
- odd and even indices.
5
+ Odd-even sort is a variant of bubble sort that alternates between
6
+ comparing consecutive odd-even and even-odd indexed pairs.
7
+
8
+ It is not of much use on a single processor as it is designed for
9
+ parallel processors, which can perform every comparison in a single pass
10
+ at the same time, thus making the algorithm much more efficient.
7
11
8
12
9
13
If the two highlighted elements are out of order, hit LEFT ARROW to swap
Original file line number Diff line number Diff line change 2
2
QUICKSORT
3
3
4
4
5
- Quicksort designates the last element as the pivot and puts everything
6
- less than the pivot before it and everything greater after it. This
7
- partitioning is done by iterating through the array while keeping track
8
- of a pointer initially set to the first element. Every time an element
9
- less than the pivot is encountered, it is swapped with the pointed
10
- element and the pointer moves forward. At the end, the pointer and pivot
11
- are swapped, and the process is repeated on the left and right halves.
5
+ Quicksort designates the last element as the pivot and sets a pointer to
6
+ the first element. Then it iterates through the array. Every time an
7
+ element smaller than the pivot is encountered, that element is swapped
8
+ with the pointed element and the pointer is incremented. Once the pivot
9
+ is reached, it is swapped with the pointed element and this process is
10
+ recursively repeated on the left and right halves.
11
+
12
+ Quicksort competes with other linearithmic algorithms like merge sort,
13
+ which it is faster than at the tradeoff of stability.
12
14
13
15
14
16
If the highlighted element is less than the pivot or the pivot has been
15
- reached, press LEFT ARROW to swap it with the pointer. Otherwise, press
16
- RIGHT ARROW to move on.
17
+ reached, press LEFT ARROW. Otherwise, press RIGHT ARROW.
17
18
"""
18
19
19
20
class_name QuickSort
Original file line number Diff line number Diff line change 2
2
SELECTION SORT
3
3
4
4
5
- Selection sort incrementally builds a sorted array by repeatedly looking
6
- for the smallest element and swapping it onto the end of the sorted
7
- portion of the array, which initially starts with size zero but grows
8
- after each round. It is faster than an unoptimized bubble sort but
9
- slower than insertion sort.
5
+ Selection sort incrementally builds a sorted subarray by finding the
6
+ smallest unprocessed element and putting it in place.
10
7
8
+ It is not very useful in real life as it is beat by insertion sort.
9
+ However, it has the distinguishing feature of making the least number
10
+ of swaps in the worst case.
11
11
12
- Keep on hitting RIGHT ARROW until you encounter an element that is
13
- smaller than the left highlighted element, then hit LEFT ARROW and
14
- repeat .
12
+
13
+ If the two highlighted elements are out of order, hit LEFT ARROW to swap
14
+ them. Otherwise, hit RIGHT ARROW to continue .
15
15
"""
16
16
17
17
Original file line number Diff line number Diff line change 2
2
SHELL SORT
3
3
4
4
5
- Shell sort is a variation of insertion sort that sorts arrays separated
6
- by gaps.
5
+ Shell sort is a variant of insertion sort that compares elements a
6
+ certain gap apart instead of consecutive elements. This gap is divided
7
+ by 2 after every pass. Once the gap becomes 1, shell sort becomes a
8
+ regular insertion sort.
7
9
10
+ This allows the final pass of insertion sort to avoid having to move
11
+ elements long distances. However, it still has a quadratic worst case,
12
+ which can be reduced with more complex gap sequences.
8
13
9
- Hit LEFT ARROW to swap the two highlighted elements as long as they are
10
- out of order. When this is no longer the case , hit RIGHT ARROW to
11
- advance .
14
+
15
+ If the two highlighted elements are out of order , hit LEFT ARROW to swap
16
+ them. Otherwise, hit RIGHT ARROW to continue .
12
17
"""
13
18
14
19
class_name ShellSort
Original file line number Diff line number Diff line change @@ -71,7 +71,9 @@ margin_top = 20.0
71
71
margin_right = 596.0
72
72
margin_bottom = 593.0
73
73
size_flags_vertical = 3
74
- text = "This is a description for the level in the form of psuedocode.
74
+ text = "This is a description of the algorithm in plain English.
75
+
76
+ This explains the relevance of the algorithm in computer science.
75
77
76
78
def algorithm(parameter):
77
79
return result"
You can’t perform that action at this time.
0 commit comments