-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A total of ten sorts are now playable! [ADDED] - Cocktail sort, shell sort, comb sort, cycle sort, and odd-even sort
- Loading branch information
Showing
24 changed files
with
461 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
COCKTAIL SORT | ||
Cocktail shaker sort is a variation of bubble sort that | ||
alternates going backwards and forwards. | ||
If the two highlighted elements are out of order, hit LEFT ARROW to swap | ||
them. Otherwise, hit RIGHT ARROW to continue. | ||
""" | ||
|
||
class_name CocktailSort | ||
extends ComparisonSort | ||
|
||
const ACTIONS = { | ||
"SWAP": "Left", | ||
"CONTINUE": "Right", | ||
} | ||
var _index = 0 # First of two elements being compared | ||
var _sorted = 0 # Size of the sorted subarray at the end of the array | ||
var _forwards = true | ||
var _swapped = false | ||
|
||
func _init(array).(array): | ||
pass | ||
|
||
func next(action): | ||
if array.at(_index) > array.at(_index + 1): | ||
if action != null and action != ACTIONS.SWAP: | ||
return emit_signal("mistake") | ||
array.swap(_index, _index + 1) | ||
_swapped = true | ||
else: | ||
if action != null and action != ACTIONS.CONTINUE: | ||
return emit_signal("mistake") | ||
if _forwards: | ||
_index += 1 | ||
if _index == array.size - _sorted - 1: | ||
if not _swapped: | ||
emit_signal("done") | ||
_swapped = false | ||
_forwards = false | ||
_index -= 2 | ||
_sorted += 1 | ||
else: | ||
_index -= 1 | ||
if _index == _sorted - 2: | ||
if not _swapped: | ||
emit_signal("done") | ||
_swapped = false | ||
_forwards = true | ||
_index += 2 | ||
|
||
func get_effect(i): | ||
if i == _index or i == _index + 1: | ||
return EFFECTS.HIGHLIGHTED | ||
if i < _sorted and _forwards == true or i < _sorted - 1 or i >= array.size - _sorted: | ||
return EFFECTS.DIMMED | ||
return EFFECTS.NONE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
COMB SORT | ||
Comb sort is a variant of bubble sort that operates on gapped arrays. | ||
If the two highlighted elements are out of order, hit LEFT ARROW to swap | ||
them. Otherwise, hit RIGHT ARROW to continue. | ||
""" | ||
|
||
class_name CombSort | ||
extends ComparisonSort | ||
|
||
const SHRINK_FACTOR = 1.3 | ||
const ACTIONS = { | ||
"SWAP": "Left", | ||
"CONTINUE": "Right", | ||
} | ||
var _gap: int = array.size / SHRINK_FACTOR | ||
var _index = 0 # First of two elements being compared | ||
var _end = array.size # Beginning of sorted subarray | ||
var _swapped = false | ||
|
||
func _init(array).(array): | ||
pass | ||
|
||
func next(action): | ||
if array.at(_index) > array.at(_index + _gap): | ||
if action != null and action != ACTIONS.SWAP: | ||
return emit_signal("mistake") | ||
array.swap(_index, _index + _gap) | ||
_swapped = true | ||
elif action != null and action != ACTIONS.CONTINUE: | ||
return emit_signal("mistake") | ||
_index += 1 | ||
if _index + _gap >= array.size: | ||
if not _swapped and _gap == 1: | ||
emit_signal("done") | ||
_gap /= SHRINK_FACTOR | ||
_gap = max(_gap, 1) | ||
_index = 0 | ||
_swapped = false | ||
|
||
func get_effect(i): | ||
if i == _index or i == _index + _gap: | ||
return EFFECTS.HIGHLIGHTED | ||
if i >= _end: | ||
return EFFECTS.DIMMED | ||
return EFFECTS.NONE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
CYCLE SORT | ||
Cycle sort repeatedly counts the number of elements less than the first | ||
and swaps it with that index until the smallest element is reached. Then | ||
it does this process starting at the next out-of-place element. | ||
If the highlighted element is less than the pointer, hit LEFT ARROW. | ||
Otherwise, hit RIGHT ARROW. | ||
""" | ||
|
||
class_name CycleSort | ||
extends ComparisonSort | ||
|
||
const ACTIONS = { | ||
"SMALLER": "Left", | ||
"BIGGER": "Right", | ||
} | ||
var _pointer = 0 | ||
var _index = 0 | ||
var _smaller = 0 | ||
|
||
func _init(array).(array): | ||
pass | ||
|
||
func next(action): | ||
if array.at(_index) < array.at(_pointer): | ||
if action != null and action != ACTIONS.SMALLER: | ||
return emit_signal("mistake") | ||
_smaller += 1 | ||
elif array.at(_index) >= array.at(_pointer): | ||
if action != null and action != ACTIONS.BIGGER: | ||
return emit_signal("mistake") | ||
_index += 1 | ||
if _index == array.size: | ||
array.swap(_pointer, _smaller) | ||
while array.at(_pointer) == _pointer + 1: | ||
_pointer += 1 | ||
if _pointer == array.size: | ||
return emit_signal("done") | ||
_index = 0 | ||
_smaller = 0 | ||
|
||
func get_effect(i): | ||
if i == _index: | ||
return EFFECTS.HIGHLIGHTED | ||
return EFFECTS.NONE | ||
|
||
func get_pointer(): | ||
return _pointer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
ODD-EVEN SORT | ||
Odd-even sort is a variant of bubble sort that alternates on elements at | ||
odd and even indices. | ||
If the two highlighted elements are out of order, hit LEFT ARROW to swap | ||
them. Otherwise, hit RIGHT ARROW to continue. | ||
""" | ||
|
||
class_name OddEvenSort | ||
extends ComparisonSort | ||
|
||
const ACTIONS = { | ||
"SWAP": "Left", | ||
"CONTINUE": "Right", | ||
} | ||
var _index = 1 | ||
var _swapped = false | ||
|
||
func _init(array).(array): | ||
pass | ||
|
||
func next(action): | ||
if array.at(_index) > array.at(_index + 1): | ||
if action != null and action != ACTIONS.SWAP: | ||
return emit_signal("mistake") | ||
array.swap(_index, _index + 1) | ||
_swapped = true | ||
elif action != null and action != ACTIONS.CONTINUE: | ||
return emit_signal("mistake") | ||
_index += 2 | ||
if _index + 1 >= array.size: | ||
if _index % 2 == 0 and not _swapped: | ||
emit_signal("done") | ||
_index = 1 if _index % 2 == 0 else 0 | ||
_swapped = false | ||
|
||
func get_effect(i): | ||
if i == _index or i == _index + 1: | ||
return EFFECTS.HIGHLIGHTED | ||
return EFFECTS.NONE |
Oops, something went wrong.