Skip to content

XSort insertionSort improvements. #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions base/src/main/java/org/eclipse/serializer/collections/XSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ private static void insertionsort0(final int[] values, final int start, final in
while(next < values[j])
{
values[j + 1] = values[j];
if(--j < 0)
if(--j < start)
break;
}
values[j + 1] = next;
Expand All @@ -548,7 +548,7 @@ private static void insertionsort0(final long[] values, final int start, final i
while(next < values[j])
{
values[j + 1] = values[j];
if(--j < 0)
if(--j < start)
break;
}
values[j + 1] = next;
Expand Down Expand Up @@ -665,7 +665,7 @@ public static final void insertionsort(final int[] values)
*/
for(int j, i = 0; i < values.length - 1; i++)
{
final int next = values[(j = i) + 1];
final int next = values[(j = i) + 1]; // must keep the next value separately since the position gets overwritten.
while(next < values[j])
{
values[j + 1] = values[j];
Expand All @@ -676,6 +676,12 @@ public static final void insertionsort(final int[] values)
}
}

public static void insertionsort(final int[] values, final int start, final int bound)
{
XArrays.validateArrayRange(values.length, start, bound - start);
insertionsort0(values, start, bound);
}

public static final void insertionsort(final long[] values)
{
// copy of insertionsort(int[]). See comment there.
Expand Down Expand Up @@ -741,6 +747,17 @@ public static final <E> void insertionsort(final E[] values, final Comparator<?
}
}

public static final <E> void insertionsort(
final E[] values ,
final Comparator<? super E> comparator,
final int start ,
final int bound
)
{
XArrays.validateArrayRange(values.length, start, bound - start);
insertionsort0(values, start, bound, comparator);
}

public static final void insertionsort(final char[] values)
{
// copy of insertionsort(int[]). See comment there.
Expand Down
Loading