Skip to content

Commit f82f003

Browse files
committed
fix: bubble sort no longer fails on reversed array
A corner case in the end condition of bubble sort meant that if the array ever got to a state where the unsorted subarray is size 2 and out of order, i.e. _end = 2 AND array[0] > array[1] (starting with a reversed array will lead to this condition), the next iteration will have _index equal _end, missing the end condition entirely and eventually causing a crash when the player goes outside the array bounds.
1 parent ff6a4d7 commit f82f003

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

levels/bubble_sort.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func next(action):
3232
_index += 1
3333
# Prevent player from having to spam tap through the end
3434
if _index + 1 == _end:
35-
if not _swapped:
35+
if not _swapped or _end == 2: # Stop if only one element left
3636
emit_signal("done")
3737
_index = 0
3838
_end -= 1

models/array_model.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func _init(size=16):
99
for i in range(1, size + 1):
1010
array.append(i)
1111
array.shuffle()
12-
self.size = size
12+
self.size = array.size()
1313

1414
func at(i):
1515
"""Retrieve the value of the element at index i."""

0 commit comments

Comments
 (0)