2018-11-14 01:15:47 +08:00
|
|
|
package Sorts;
|
2018-04-09 20:48:08 +08:00
|
|
|
|
2018-11-14 01:15:47 +08:00
|
|
|
import static Sorts.SortUtils.less;
|
|
|
|
import static Sorts.SortUtils.print;
|
2018-04-09 20:48:08 +08:00
|
|
|
|
|
|
|
/**
|
2019-05-09 19:32:54 +08:00
|
|
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
2018-04-09 20:48:08 +08:00
|
|
|
*/
|
|
|
|
class CycleSort implements SortAlgorithm {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T extends Comparable<T>> T[] sort(T[] arr) {
|
|
|
|
int n = arr.length;
|
|
|
|
|
|
|
|
// traverse array elements
|
|
|
|
for (int j = 0; j <= n - 2; j++) {
|
|
|
|
// initialize item as starting point
|
|
|
|
T item = arr[j];
|
|
|
|
|
|
|
|
// Find position where we put the item.
|
|
|
|
int pos = j;
|
|
|
|
for (int i = j + 1; i < n; i++)
|
|
|
|
if (less(arr[i], item)) pos++;
|
|
|
|
|
|
|
|
// If item is already in correct position
|
2019-05-09 19:32:54 +08:00
|
|
|
if (pos == j) continue;
|
2018-04-09 20:48:08 +08:00
|
|
|
|
|
|
|
// ignore all duplicate elements
|
|
|
|
while (item.compareTo(arr[pos]) == 0)
|
|
|
|
pos += 1;
|
|
|
|
|
|
|
|
// put the item to it's right position
|
|
|
|
if (pos != j) {
|
|
|
|
item = replace(arr, pos, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate rest of the cycle
|
|
|
|
while (pos != j) {
|
|
|
|
pos = j;
|
|
|
|
|
|
|
|
// Find position where we put the element
|
|
|
|
for (int i = j + 1; i < n; i++)
|
2019-05-09 19:32:54 +08:00
|
|
|
if (less(arr[i], item)) {
|
2018-04-09 20:48:08 +08:00
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ignore all duplicate elements
|
|
|
|
while (item.compareTo(arr[pos]) == 0)
|
|
|
|
pos += 1;
|
|
|
|
|
|
|
|
// put the item to it's right position
|
|
|
|
if (item != arr[pos]) {
|
|
|
|
item = replace(arr, pos, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2019-05-09 19:32:54 +08:00
|
|
|
private <T extends Comparable<T>> T replace(T[] arr, int pos, T item) {
|
2018-04-09 20:48:08 +08:00
|
|
|
T temp = item;
|
|
|
|
item = arr[pos];
|
|
|
|
arr[pos] = temp;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2019-05-09 19:32:54 +08:00
|
|
|
Integer arr[] = {4, 23, 6, 78, 1, 26, 11, 23, 0, -6, 3, 54, 231, 9, 12};
|
2018-04-09 20:48:08 +08:00
|
|
|
CycleSort cycleSort = new CycleSort();
|
|
|
|
cycleSort.sort(arr);
|
|
|
|
|
|
|
|
System.out.println("After sort : ");
|
|
|
|
print(arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|