From dc5cd409bb486b4df7955ae7993e3d73ca16f706 Mon Sep 17 00:00:00 2001 From: Bonajo Date: Wed, 28 Oct 2026 12:13:26 +0100 Subject: [PATCH 1/2] feat: add KeySorter and SortKind.RADIX A KeySorter sorts on a non-negative int key instead of a Comparator, which allows linear-time sorting (radix sort). SorterConfiguration gets usesKeySorter() and getKeySorter() as defaults, so existing configurations keep compiling. Bumps the version to 1.3.0. --- pom.xml | 2 +- src/main/java/sortingservice/KeySorter.java | 37 +++++++++++++++++++ src/main/java/sortingservice/SortKind.java | 6 ++- .../sortingservice/SorterConfiguration.java | 18 +++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/main/java/sortingservice/KeySorter.java diff --git a/pom.xml b/pom.xml index 5fe8f87..ab0454b 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ alda_sorting_api - 1.2.0 + 1.3.0 jar Sorting Service API diff --git a/src/main/java/sortingservice/KeySorter.java b/src/main/java/sortingservice/KeySorter.java new file mode 100644 index 0000000..ee40c8e --- /dev/null +++ b/src/main/java/sortingservice/KeySorter.java @@ -0,0 +1,37 @@ +package sortingservice; + +import java.util.function.ToIntFunction; + +/** + * A key sorter sorts its input on an integer key, without comparing elements. + * + *

+ * Where a {@link Sorter} can only compare two elements with a + * {@link java.util.Comparator Comparator}, a key sorter gets access to the + * key of every element. This restriction on the input allows sorting in + * linear time, e.g. with radix sort. + *

+ * + * The implementation is expected to return the same queue instance. + * + * @author Martijn Bonajo + * @param type of elements in queue. + */ +@FunctionalInterface +public interface KeySorter { + + /** + * Return the input queue in sorted order, based on the key of the elements. + * + *

+ * The sort is stable: elements with equal keys keep their relative + * order. + *

+ * + * @param queue to be sorted + * @param key extracts the key of an element, the key must be non-negative + * @return the queue with the elements in non-descending order of their key. + * @throws IllegalArgumentException if the key of an element is negative. + */ + Queue sort( Queue queue, ToIntFunction key ); +} diff --git a/src/main/java/sortingservice/SortKind.java b/src/main/java/sortingservice/SortKind.java index e0c5f67..671a62b 100644 --- a/src/main/java/sortingservice/SortKind.java +++ b/src/main/java/sortingservice/SortKind.java @@ -23,5 +23,9 @@ public enum SortKind { /** * Heap sort. */ - HEAP; + HEAP, + /** + * Radix sort. + */ + RADIX; } diff --git a/src/main/java/sortingservice/SorterConfiguration.java b/src/main/java/sortingservice/SorterConfiguration.java index 6e27d1a..59c9b30 100644 --- a/src/main/java/sortingservice/SorterConfiguration.java +++ b/src/main/java/sortingservice/SorterConfiguration.java @@ -50,6 +50,24 @@ public interface SorterConfiguration { */ Sorter getSorter(); + /** + * Indicates whether this sorter uses a key sorter. + * @return true if configuration uses key sorter, false by default. + */ + default boolean usesKeySorter() { + return false; + } + + /** + * Get a key sorter. + * @param the type of elements in the queue + * @return key sorter or null if configuration does not use a key sorter, + * null by default. + */ + default KeySorter getKeySorter() { + return null; + } + /** * Indicates whether the teacher tests should test this configuration. * Your own tests should test all configurations. From 823df62771086c5db593d6584c4e6c3c34b3d008 Mon Sep 17 00:00:00 2001 From: Bonajo Date: Thu, 29 Oct 2026 11:47:05 +0100 Subject: [PATCH 2/2] docs: say what getQueue() and getSorter() return for special configurations getQueue() returns null for a configuration with a priority queue. getSorter() returns a pass-through sorter for a priority queue configuration, and null for one with a key sorter. --- src/main/java/sortingservice/SorterConfiguration.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/sortingservice/SorterConfiguration.java b/src/main/java/sortingservice/SorterConfiguration.java index 59c9b30..374f87f 100644 --- a/src/main/java/sortingservice/SorterConfiguration.java +++ b/src/main/java/sortingservice/SorterConfiguration.java @@ -30,7 +30,8 @@ public interface SorterConfiguration { /** * Get queue for sorter. * @param the type of elements in the queue - * @return queue for sorter + * @return queue for sorter, or null if configuration uses a priority + * queue (see {@link #getPriorityQueue(Comparator)}). */ Queue getQueue(); @@ -44,9 +45,12 @@ public interface SorterConfiguration { PriorityQueue getPriorityQueue(Comparator comparator); /** - * Get a sorter. + * Get a sorter. + * A configuration that uses a priority queue returns a sorter that + * returns the queue as is, since the priority queue sorts by itself. * @param the type of elements in the queue - * @return sorter + * @return sorter, or null if configuration uses a key sorter (see + * {@link #getKeySorter()}). */ Sorter getSorter();