Shell Sort
package iteration;
public interface Iterable {
public Iterator iterator();
}
package dataStructures;
import iteration.Iterable;
public interface List extends Iterable {
public void insert(int index, Object value)
throws IndexOutOfBoundsException;
public void add(Object value);
public Object delete(int index) throws IndexOutOfBoundsException;
public boolean delete(Object value);
public void clear();
public Object set(int index, Object value)
throws IndexOutOfBoundsException;
public Object get(int index) throws IndexOutOfBoundsException;
public int indexOf(Object value);
public boolean contains(Object value);
public int size();
public boolean isEmpty();
}
package sorting;
import dataStructures.List;
public interface ListSorter {
public List sort(List list);
}
package sorting;
import dataStructures.List;
public class ShellsortListSorter implements ListSorter {
private final Comparator _comparator;
private final int[] _increments = {121, 40, 13, 4, 1};
public ShellsortListSorter(Comparator comparator) {
assert comparator != null : "comparator cannot be null";
_comparator = comparator;
}
public List sort(List list) {
assert list != null : "list cannot be null";
for (int i = 0; i < _increments.length; i++) {
int increment = _increments[i];
hSort(list, increment);
}
return list;
}
private void hSort(List list, int increment) {
if (list.size() < (increment * 2)) {
return;
}
for (int i = 0; i < increment; ++i) {
sortSublist(list, i, increment);
}
}
private void sortSublist(List list, int startIndex, int increment) {
for (int i = startIndex + increment; i < list.size(); i += increment) {
Object value = list.get(i);
int j;
for (j = i; j > startIndex; j -= increment) {
Object previousValue = list.get(j - increment);
if (_comparator.compare(value, previousValue) >= 0) {
break;
}
list.set(j, previousValue);
}
list.set(j, value);
}
}
}