|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--com.mhhe.clrs2e.CountingSort
Sorts an array of NonNegativeInteger via the counting sort
algorithm from page 168 of Introduction to Algorithms,
Second edition.
| Nested Class Summary | |
private static class |
CountingSort.CountingSortKeyExtractor
Inner class implementing KeyExtractor, in which
each extract method just returns its argument. |
static interface |
CountingSort.KeyExtractor
Interface that allows us to pull out the sort key from an object. |
| Field Summary | |
private CountingSort.KeyExtractor |
extractor
Used to generate the actual key. |
private int |
max
Maximum value in the array. |
private int |
NO_MAX
Indicates that no maximum was set. |
| Constructor Summary | |
CountingSort()
Initializes max and extractor to
defaults. |
|
CountingSort(CountingSort.KeyExtractor extractor)
Initializes max to the default and
extractor to its argument. |
|
CountingSort(CountingSort.KeyExtractor extractor,
int max)
Initializes max and extractor to its
arguments. |
|
CountingSort(int max)
Initializes max to its argument and
extractor to the default. |
|
| Method Summary | |
void |
countingSort(com.mhhe.clrs2e.NonNegativeInteger[] array)
Sorts an array of NonNegativeInteger. |
void |
countingSort(com.mhhe.clrs2e.NonNegativeInteger[] array,
int k)
Sorts an array of NonNegativeInteger, given the
maximum value in the array. |
void |
countingSort(com.mhhe.clrs2e.NonNegativeInteger[] array,
com.mhhe.clrs2e.NonNegativeInteger[] sorted,
int k)
Sorts an array of NonNegativeInteger, given the
maximum value in the array and array to sort into. |
int |
findMax(com.mhhe.clrs2e.NonNegativeInteger[] array)
Returns the maximum value in an array of NonNegativeInteger. |
void |
setExtractor(CountingSort.KeyExtractor extractor)
Changes the extractor to its argument. |
void |
setMax(int max)
Changes the maximum to its argument. |
void |
sort(com.mhhe.clrs2e.NonNegativeInteger[] array)
Sorts an array of NonNegativeInteger. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
private final int NO_MAX
private int max
private CountingSort.KeyExtractor extractor
| Constructor Detail |
public CountingSort()
max and extractor to
defaults.
public CountingSort(CountingSort.KeyExtractor extractor)
max to the default and
extractor to its argument.
extractor - The KeyExtractor to use.public CountingSort(int max)
max to its argument and
extractor to the default.
max - Value of max to use.
public CountingSort(CountingSort.KeyExtractor extractor,
int max)
max and extractor to its
arguments.
max - Value of max to use.extractor - The KeyExtractor to use.| Method Detail |
public void setExtractor(CountingSort.KeyExtractor extractor)
extractor - The new KeyExtractor to use.public void setMax(int max)
max - The new value of max to use.public int findMax(com.mhhe.clrs2e.NonNegativeInteger[] array)
NonNegativeInteger.
array - The array of NonNegativeInteger.public void sort(com.mhhe.clrs2e.NonNegativeInteger[] array)
NonNegativeInteger.
array - The array of NonNegativeInteger to be
sorted.public void countingSort(com.mhhe.clrs2e.NonNegativeInteger[] array)
NonNegativeInteger. If
max has not been set, calculates it by finding the
maximum value in the array in a preprocessing step.
array - The array of NonNegativeInteger to be
sorted.
java.lang.ArrayIndexOutOfBoundsException - if the array to be
sorted contains a negative value or a value greater than the
presumed maximum value.
public void countingSort(com.mhhe.clrs2e.NonNegativeInteger[] array,
int k)
NonNegativeInteger, given the
maximum value in the array.
array - The array of NonNegativeInteger to be
sorted.k - Maximum value in the array.
java.lang.ArrayIndexOutOfBoundsException - if the array to be
sorted contains a negative value or a value greater than the
presumed maximum value.
public void countingSort(com.mhhe.clrs2e.NonNegativeInteger[] array,
com.mhhe.clrs2e.NonNegativeInteger[] sorted,
int k)
NonNegativeInteger, given the
maximum value in the array and array to sort into.
array - The array of NonNegativeInteger to be
sorted.sorted - Output array; corresponds to the output array B
in the pseudocode on page 168 and contains a sorted version
of array.k - Maximum value in the array.
java.lang.ArrayIndexOutOfBoundsException - if the array to be
sorted contains a negative value or a value greater than the
presumed maximum value.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||