Skip to content

Commit 4523ec4

Browse files
committed
Merge: Fixed quick_sort when array is length 0 or 1
Fixed a bug where quick_sort could access out of bound element when `to` argument smaller or equal to 0. # Before var xs = [1] default_comparator.quick_sort(xs) # assertion failed # After var xs = [1] default_comparator.quick_sort(xs) assert xs == [1] # true Signed-off-by: Louis-Vincent Boudreault <[email protected]> Pull-Request: #2811
2 parents 9c25a1a + 6c134b3 commit 4523ec4

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/core/collection/sorter.nit

+7
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,14 @@ interface Comparator
106106
# var a = [5, 2, 3, 1, 4]
107107
# default_comparator.quick_sort(a, 0, a.length - 1)
108108
# assert a == [1, 2, 3, 4, 5]
109+
# var a2 = new Array[Int]
110+
# default_comparator.quick_sort(a2, 0, a2.length - 1)
111+
# assert a2 == new Array[Int]
112+
# var a3 = [1]
113+
# default_comparator.quick_sort(a3, 0, a3.length - 1)
114+
# assert a3 == [1]
109115
fun quick_sort(array: Array[COMPARED], from: Int, to: Int) do
116+
if from >= to then return
110117
var pivot = array[from]
111118
var i = from
112119
var j = to

0 commit comments

Comments
 (0)