Java tutorial
//package com.java2s; import java.util.Comparator; import java.util.List; public class Main { /** * A shell sort * * @author Jason Harrison */ public static <T extends Comparable<T>> void shellSort(List<T> list) { int h = 1; /* find the largest h value possible */ while ((h * 3 + 1) < list.size()) { h = 3 * h + 1; } /* * while h remains larger than 0 */ while (h > 0) { /* * for each set of elements (there are h sets) */ for (int i = h - 1; i < list.size(); i++) { /* * pick the last element in the set */ T A; T B = list.get(i); int j = i; /* * compare the element at B to the one before it in the set * if they are out of order continue this loop, moving * elements "back" to make room for B to be inserted. */ for (j = i; (j >= h) && ((A = list.get(j - h)).compareTo(B) > 0); j -= h) { list.set(j, A); } /* * insert B into the correct place */ list.set(j, B); } /* * all sets h-sorted, now decrease set size */ h = h / 3; } } /** * A shell sort * * @author Jason Harrison */ public static <T> void shellSort(List<T> list, Comparator<? super T> c) { int h = 1; /* find the largest h value possible */ while ((h * 3 + 1) < list.size()) { h = 3 * h + 1; } /* * while h remains larger than 0 */ while (h > 0) { /* * for each set of elements (there are h sets) */ for (int i = h - 1; i < list.size(); i++) { /* * pick the last element in the set */ T A; T B = list.get(i); int j = i; /* * compare the element at B to the one before it in the set * if they are out of order continue this loop, moving * elements "back" to make room for B to be inserted. */ for (j = i; (j >= h) && (c.compare((A = list.get(j - h)), B) > 0); j -= h) { list.set(j, A); } /* * insert B into the correct place */ list.set(j, B); } /* * all sets h-sorted, now decrease set size */ h = h / 3; } } }