Eight kinds of ordering relationships:

First, basic ideas

The first set of numbers to be sorted by the algorithm is divided into several groups by some increment D (n/2, where n is the number of numbers to be sorted), and the subscript difference in each group is D. Direct insert sort is done for all elements in each group, and then it is grouped by a smaller increment (D /2), and direct insert sort is done again in each group. When the increment is reduced to 1, the sort is complete after direct insert sort.

package com.chb.sort; Public class ShellSort {public static void main(String[]) public static void main(String[]) args) { shellSort(a); } the static int a [] =,54,6,3,78,34,12,45,56,100 {1}; public static void shellSort(int[] a){ int d = a.length; while(true) { d = (int) Math.ceil(d/2); for (int i = 0; i <= d; I++) {// // insert sort j j+d j+2d... for (int j = i; j < a.length-d; j+=d) { int k = j; Int insertNum = a[k+d]; / / to insert data while (k > = j & a [k] > insertNum) {a [+ d] k = a, [k]. k-=d; }// if ((k+d)) //if (k+d)! = j) { a[k+d] = insertNum; //} } } if (d == 1) { break; } } for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); }}}Copy the code