Skip to content

Commit 942384d

Browse files
Fix: Handle reverse-sorted lists with duplicates in presorted check
- Updated `_handlePresorted` and '_handlePresortedBy' to treat non-increasing sequences (e.g., [3, 2, 2, 1]) as reverse-sorted, allowing O(n) reversal instead of O(n log n) sorting.
1 parent 8029a6a commit 942384d

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

pkgs/collection/lib/src/algorithms.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ bool _handlePresorted<E>(
587587
if (compare(elements[start], elements[start + 1]) > 0) {
588588
// Check strictly decreasing
589589
var i = start + 1;
590-
while (i < end && compare(elements[i - 1], elements[i]) > 0) {
590+
while (i < end && compare(elements[i - 1], elements[i]) >= 0) {
591591
i++;
592592
}
593593
if (i == end) {
@@ -771,7 +771,7 @@ bool _handlePresortedBy<E, K>(List<E> elements, K Function(E) keyOf,
771771
int Function(K, K) compare, int start, int end) {
772772
if (compare(keyOf(elements[start]), keyOf(elements[start + 1])) > 0) {
773773
var i = start + 1;
774-
while (i < end && compare(keyOf(elements[i - 1]), keyOf(elements[i])) > 0) {
774+
while (i < end && compare(keyOf(elements[i - 1]), keyOf(elements[i])) >= 0) {
775775
i++;
776776
}
777777
if (i == end) {

0 commit comments

Comments
 (0)