File tree Expand file tree Collapse file tree 12 files changed +92
-57
lines changed Expand file tree Collapse file tree 12 files changed +92
-57
lines changed Original file line number Diff line number Diff line change 22BUBBLE SORT
33
44
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.
1015
1116
1217If the two highlighted elements are out of order, hit LEFT ARROW to swap
Original file line number Diff line number Diff line change 22COCKTAIL SORT
33
44
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.
711
812
913If the two highlighted elements are out of order, hit LEFT ARROW to swap
@@ -22,10 +26,8 @@ def cocktail_sort(a):
2226 if array[i] > array[i + 1]:
2327 a.swap(i, i + 1)
2428 swapped = true
25-
2629 if not swapped:
2730 break
28-
2931 swapped = false
3032 for i in range(len(a) - 1, 0, -1)
3133 if a[i - 1] > a[i]:
Original file line number Diff line number Diff line change 22COMB SORT
33
44
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.
613
714
815If the two highlighted elements are out of order, hit LEFT ARROW to swap
@@ -17,6 +24,7 @@ def comb_sort(a):
1724 gap = len(a)
1825 swapped = true
1926 while gap != 1 or swapped:
27+ swapped = false
2028 gap = max(gap / 1.3, 1)
2129 for i in range(len(a) - gap):
2230 if a[i] > a[i + gap]:
Original file line number Diff line number Diff line change @@ -12,8 +12,8 @@ const EFFECTS = {
1212
1313const DISABLE_TIME = 1.0
1414var 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 ]
1717
1818var array : ArrayModel
1919
Original file line number Diff line number Diff line change 22CYCLE SORT
33
44
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.
810
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.
915
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.
1219"""
1320
1421class_name CycleSort
Original file line number Diff line number Diff line change 22INSERTION SORT
33
44
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.
107
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.
1112
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 .
1516"""
1617
1718class_name InsertionSort
Original file line number Diff line number Diff line change 22MERGE SORT
33
44
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 .
1010
1111
1212Press 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.
1514"""
1615
1716class_name MergeSort
@@ -34,6 +33,7 @@ def merge_sort(a):
3433 merged.append(a[i])
3534 i += 1
3635 a[begin:begin + size] = merged
36+ size *= 2
3737"""
3838const ACTIONS = {
3939 "LEFT" : "Left" ,
Original file line number Diff line number Diff line change 22ODD-EVEN SORT
33
44
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.
711
812
913If the two highlighted elements are out of order, hit LEFT ARROW to swap
Original file line number Diff line number Diff line change 22QUICKSORT
33
44
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.
1214
1315
1416If 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.
1718"""
1819
1920class_name QuickSort
Original file line number Diff line number Diff line change 22SELECTION SORT
33
44
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.
107
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.
1111
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 .
1515"""
1616
1717
You can’t perform that action at this time.
0 commit comments