Loading…
Loading…
A gap-based generalization of insertion sort: sort elements far apart first, shrinking the gap until it becomes a final pass of ordinary insertion sort.
Gap = 8.
1void shellSort(int[] a) {2 int n = a.length;3 for (int gap = n/2; gap > 0; gap /= 2)4 for (int i = gap; i < n; i++) {5 int key = a[i], j = i;6 while (j >= gap && a[j-gap] > key) {7 a[j] = a[j-gap]; j -= gap;8 }9 a[j] = key;10 }11}