import edu.princeton.cs.algs4.StdIn;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* @author LFool
* @create 2020-07-24 20:20
*/
@SuppressWarnings({"rawtypes"})
public class Shell {
private Shell() {
}
/**
* Rearranges the array in ascending order, using the natural order
* @param arr the array to be sorted
*/
public static void sort(Comparable[] arr) {
int n = arr.length;
// 3x+1 increment sequence: 1, 4, 13, 40, 121, 364, 1093, ...
int h = 1;
while (h < n / 3) {
h = 3 * h + 1;
}
while (h >= 1) {
// h-sort the array
for (int i = h; i < n; i++) {
for (int j = i; j >= h && SortUtil.less(arr[j], arr[j - 1]); j--) {
SortUtil.exch(arr, j, j - 1);
}
}
assert SortUtil.isHSorted(arr, h);
h /= 3;
}
assert SortUtil.isSorted(arr);
}
/**
* Reads in a sequence of strings from standard input; selection sorts them;
* and prints them to standard output in ascending order.
*
* @param args the command-line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
FileInputStream input = new FileInputStream("src/main/resources/" + "tiny.txt");
System.setIn(input);
String[] arr = StdIn.readAllStrings();
Shell.sort(arr);
SortUtil.show(arr);
}
}