|
| 1 | +class_name QuickSort |
| 2 | +extends ComparisonSort |
| 3 | + |
| 4 | +const NAME = "QUICKSORT" |
| 5 | +const ABOUT = """ |
| 6 | +Quicksort designates the last element as the pivot and puts everything |
| 7 | +less than the pivot before it and everything greater after it. This |
| 8 | +partitioning is done by iterating through the array while keeping track |
| 9 | +of a pointer initially set to the first element. Every time an element |
| 10 | +less than the pivot is encountered, it is swapped with the pointed |
| 11 | +element and the pointer moves forward. At the end, the pointer and pivot |
| 12 | +are swapped, and the process is repeated on the left and right halves. |
| 13 | +""" |
| 14 | +const CONTROLS = """ |
| 15 | +If the highlighted element is less than the pivot or the pivot has been |
| 16 | +reached, press LEFT ARROW to swap it with the pointer. Otherwise, press |
| 17 | +RIGHT ARROW to move on. |
| 18 | +""" |
| 19 | + |
| 20 | +const ACTIONS = { |
| 21 | + "SWAP": "Left", |
| 22 | + "CONTINUE": "Right", |
| 23 | +} |
| 24 | +var _index = 0 |
| 25 | +var _pointer = 0 |
| 26 | +# Bookkeep simulated recursion with a binary tree of subarray bounds |
| 27 | +var _stack = BinaryTreeModel.new(Vector2(0, array.size - 1)) |
| 28 | + |
| 29 | +func _init(array).(array): |
| 30 | + pass |
| 31 | + |
| 32 | +func next(action): |
| 33 | + if _index == _pivot(): |
| 34 | + if action != null and action != ACTIONS.SWAP: |
| 35 | + return emit_signal("mistake") |
| 36 | + array.swap(_index, _pointer) |
| 37 | + if _pointer - _begin() > 1: |
| 38 | + _stack.left = BinaryTreeModel.new(Vector2(_begin(), _pointer - 1)) |
| 39 | + _stack = _stack.left |
| 40 | + elif _pivot() - _pointer > 1: |
| 41 | + _stack.right = BinaryTreeModel.new(Vector2(_pointer + 1, _pivot())) |
| 42 | + _stack = _stack.right |
| 43 | + else: |
| 44 | + while (_stack.parent.right != null |
| 45 | + or _stack.parent.left.value.y + 2 >= _stack.parent.value.y): |
| 46 | + _stack = _stack.parent |
| 47 | + if _stack.parent == null: |
| 48 | + return emit_signal("done") |
| 49 | + _stack.parent.right = BinaryTreeModel.new(Vector2( |
| 50 | + _stack.parent.left.value.y + 2, _stack.parent.value.y)) |
| 51 | + _stack = _stack.parent.right |
| 52 | + _index = _begin() |
| 53 | + _pointer = _index |
| 54 | + elif array.at(_index) < array.at(_pivot()): |
| 55 | + if action != null and action != ACTIONS.SWAP: |
| 56 | + return emit_signal("mistake") |
| 57 | + array.swap(_index, _pointer) |
| 58 | + _index += 1 |
| 59 | + _pointer += 1 |
| 60 | + else: |
| 61 | + if action != null and action != ACTIONS.CONTINUE: |
| 62 | + return emit_signal("mistake") |
| 63 | + _index += 1 |
| 64 | + |
| 65 | +func _begin(): |
| 66 | + return _stack.value.x |
| 67 | + |
| 68 | +func _pivot(): |
| 69 | + return _stack.value.y |
| 70 | + |
| 71 | +func get_effect(i): |
| 72 | + if i < _begin() or i > _pivot(): |
| 73 | + return EFFECTS.DIMMED |
| 74 | + if i == _index or i == _pivot(): |
| 75 | + return EFFECTS.HIGHLIGHTED |
| 76 | + return EFFECTS.NONE |
| 77 | + |
| 78 | +func get_pointer(): |
| 79 | + return _pointer |
0 commit comments