Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package p 1 . sort.radix; import static org.tudalgo.algoutils.student.Student.crash; / * * * A { @link RadixIndexExtractor } for extracting the index corresponding to a character
package psort.radix;
import static org.tudalgo.algoutils.student.Student.crash;
A @link RadixIndexExtractor for extracting the index corresponding to a character in a string.
It is caseinsensitive. It maps the characters a to z to the indices to All other characters are mapped to
The position is interpreted as the position from the end of the string, ie position corresponds to the last
character in the string.
public class LatinStringIndexExtractor implements RadixIndexExtractor
@Override
public int extractIndexString value, int position
return crash; TODO: H a remove if implemented
@Override
public int getRadix
return za;
package psort.radix;
import psort.Sort;
import psort.SortList;
import static org.tudalgo.algoutils.student.Student.crash;
An implementation of the radix sort algorithm.
It is a noncomparisonbased sorting algorithm that sorts the elements by assigning them into buckets depending
on the value of the element at the currently considered position. If all elements have been assigned to the buckets,
the elements are collected from the buckets, starting at the first bucket, and the process is repeated for the next position.
The process is repeated until the every element of every value has been considered. The amount of iterations needed
is equal to @link #maxInputLength The index associated with an element at a position in a value is extracted using
the given @link RadixIndexExtractor
@param the type of the elements to be sorted.
@see Bucket
@see RadixIndexExtractor
public class RadixSort implements Sort
The extractor used for mapping the element of a value at a given position to an index in the @link #buckets array.
private final RadixIndexExtractor indexExtractor;
The buckets used for sorting the elements.
private final Bucket buckets;
The maximum amount of elements that any value in the sorted @link SortList contains.
It is equal to the lowest number @code a where @code indexExtractor.extractIndexvalue a
for all values in the sorted @link SortList
It is used for determining the amount of iterations needed to sort the list.
private int maxInputLength;
Creates a new @link RadixSort instance.
@param radix The amount of buckets to use.
@param indexExtractor The extractor used for extracting the key index to insert the elements into the buckets.
@SuppressWarningsunchecked
public RadixSortint radix, RadixIndexExtractor indexExtractor
this.indexExtractor indexExtractor;
this.buckets new Bucketradix;
if radix
throw new IllegalArgumentExceptionThe radix must be greater than ;
if radix indexExtractor.getRadix
throw new IllegalArgumentExceptionThe given radix may not be less than the radix of the keyExtractor.";
for int i ; i radix; i
bucketsi new BucketLinkedList;
@Override
public void sortSortList sortList
crash; TODO: H b remove if implemented
Adds the given value to one of the @link #buckets
The index of the bucket is determined using the @link #indexExtractor with the given value and position.
@param value The value to add to the buckets.
@param position The position used to determine the index in the @link #buckets array. The lowest position is
@throws IndexOutOfBoundsException if the position is less than
@see RadixIndexExtractor#extractIndexT int
public void putBucketT value, int position
crash; TODO: H b remove if implemented
@Override
public int getComparisonsCount
return ; Radix sort is not based on comparisons.
Sets the maximum amount of elements that any value in the @link SortList that will be sorted, contains.
@param maxInputLength the new maximum input length.
@see #maxInputLength
public void setMaxInputLengthint maxInputLength
this.maxInputLength maxInputLength;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started