mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 10:15:51 +08:00
refactor: cleanup CycleSort
(#5321)
This commit is contained in:
@ -9,74 +9,79 @@ class CycleSort implements SortAlgorithm {
|
|||||||
/**
|
/**
|
||||||
* Sorts an array of comparable elements using the cycle sort algorithm.
|
* Sorts an array of comparable elements using the cycle sort algorithm.
|
||||||
*
|
*
|
||||||
* @param array the array to be sorted
|
* @param <T> The type of elements in the array, which must be comparable
|
||||||
* @param <T> the type of elements in the array, must be comparable
|
* @param array The array to be sorted
|
||||||
* @return the sorted array
|
* @return The sorted array
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public <T extends Comparable<T>> T[] sort(T[] array) {
|
public <T extends Comparable<T>> T[] sort(final T[] array) {
|
||||||
for (int cycleStart = 0; cycleStart <= array.length - 2; cycleStart++) {
|
final int length = array.length;
|
||||||
|
|
||||||
|
for (int cycleStart = 0; cycleStart <= length - 2; cycleStart++) {
|
||||||
T item = array[cycleStart];
|
T item = array[cycleStart];
|
||||||
|
int pos = findPosition(array, cycleStart, item);
|
||||||
|
|
||||||
// Find the position where we put the element
|
|
||||||
int pos = cycleStart;
|
|
||||||
for (int i = cycleStart + 1; i < array.length; i++) {
|
|
||||||
if (SortUtils.less(array[i], item)) {
|
|
||||||
pos++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the item is already in the correct position
|
|
||||||
if (pos == cycleStart) {
|
if (pos == cycleStart) {
|
||||||
continue;
|
continue; // Item is already in the correct position
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore all duplicate elements
|
item = placeItem(array, item, pos);
|
||||||
while (item.compareTo(array[pos]) == 0) {
|
|
||||||
pos++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put the item to its correct position
|
|
||||||
if (pos != cycleStart) {
|
|
||||||
item = replace(array, pos, item);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rotate the rest of the cycle
|
// Rotate the rest of the cycle
|
||||||
while (pos != cycleStart) {
|
while (pos != cycleStart) {
|
||||||
pos = cycleStart;
|
pos = findPosition(array, cycleStart, item);
|
||||||
|
item = placeItem(array, item, pos);
|
||||||
// Find the position where we put the element
|
|
||||||
for (int i = cycleStart + 1; i < array.length; i++) {
|
|
||||||
if (SortUtils.less(array[i], item)) {
|
|
||||||
pos++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore all duplicate elements
|
|
||||||
while (item.compareTo(array[pos]) == 0) {
|
|
||||||
pos++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put the item to its correct position
|
|
||||||
if (item != array[pos]) {
|
|
||||||
item = replace(array, pos, item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the correct position for the given item starting from cycleStart.
|
||||||
|
*
|
||||||
|
* @param <T> The type of elements in the array, which must be comparable
|
||||||
|
* @param array The array to be sorted
|
||||||
|
* @param cycleStart The starting index of the cycle
|
||||||
|
* @param item The item whose position is to be found
|
||||||
|
* @return The correct position of the item
|
||||||
|
*/
|
||||||
|
private <T extends Comparable<T>> int findPosition(final T[] array, final int cycleStart, final T item) {
|
||||||
|
int pos = cycleStart;
|
||||||
|
for (int i = cycleStart + 1; i < array.length; i++) {
|
||||||
|
if (SortUtils.less(array[i], item)) {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Places the item in its correct position, handling duplicates, and returns the displaced item.
|
||||||
|
*
|
||||||
|
* @param <T> The type of elements in the array, which must be comparable
|
||||||
|
* @param array The array being sorted
|
||||||
|
* @param item The item to be placed
|
||||||
|
* @param pos The position where the item is to be placed
|
||||||
|
* @return The displaced item
|
||||||
|
*/
|
||||||
|
private <T extends Comparable<T>> T placeItem(final T[] array, final T item, int pos) {
|
||||||
|
while (item.compareTo(array[pos]) == 0) {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
return replace(array, pos, item);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces an element in the array with the given item and returns the replaced item.
|
* Replaces an element in the array with the given item and returns the replaced item.
|
||||||
*
|
*
|
||||||
* @param array the array in which the replacement will occur
|
* @param <T> The type of elements in the array, which must be comparable
|
||||||
* @param pos the position at which the replacement will occur
|
* @param array The array in which the replacement will occur
|
||||||
* @param item the item to be placed in the array
|
* @param pos The position at which the replacement will occur
|
||||||
* @param <T> the type of elements in the array, must be comparable
|
* @param item The item to be placed in the array
|
||||||
* @return the replaced item
|
* @return The replaced item
|
||||||
*/
|
*/
|
||||||
private <T extends Comparable<T>> T replace(T[] array, int pos, T item) {
|
private <T extends Comparable<T>> T replace(final T[] array, final int pos, final T item) {
|
||||||
T replacedItem = array[pos];
|
final T replacedItem = array[pos];
|
||||||
array[pos] = item;
|
array[pos] = item;
|
||||||
return replacedItem;
|
return replacedItem;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user