chocoteam / choco-solver

An open-source Java library for Constraint Programming
http://choco-solver.org/
BSD 4-Clause "Original" or "Old" License
690 stars 143 forks source link

[BUG] IntQuickSort.sort may behave incorrectly if left > 0 #1111

Closed niemisto closed 1 week ago

niemisto commented 3 weeks ago

Describe the bug IntQuickSort.sort might move some elements from the range [left,right] to the range [0,left-1] during insertion sort. This results both undesired modification in range [0,left-1] and range [left,right] not being in order after the method returns.

To Reproduce

int[] array = new int[] { 4, 2, 3, 1  };
IntQuickSort.sort(array, 1, array.length-1, Integer::compare);
System.out.println(Arrays.toString(array));

This prints [1, 4, 2, 3] instead of expected [4, 1, 2, 3].

Possible solution The bug seems to be the following line in IntQuickSort.insertionSort:

                while (i > -1 && comparator.compare(key, array[i]) < 0);

Instead of i > -1 the comparison should be i >= left.

Environment:

cprudhom commented 3 weeks ago

Can you give the full path of the class IntQuickSort?

niemisto commented 3 weeks ago

My mistake. Reported this to a wrong project. The bug was in eclipse-collections.