First, the moving map presentation

Second, the idea of analysis

Shell sort is the subject of pressing the recording packet in increments, the use of direct insertion sort each sorting algorithm; With increment is gradually reduced, more and more keywords each comprising, when reduced to an increment, the entire file is divided into just one set, the algorithm will terminate.

Behave very simple insertion sort, regardless of the distribution of the array is how still the elements step by step by comparing movement, insertion, such as [5,4,3,2,1,0] This reverse sequence, end of the array 0 to go back to the first position is very strenuous, compare and move elements required n-1 times.

And Hill sorting strategy employed in the array jump packet, by a delta array element may be divided into several groups, and packet insertion sort, then gradually reduced incremental insertion sorting operation continues by group until the increment 1. Hill sorting through this strategy makes the entire array from a macro perspective basically in order to reach the small base before the larger base after the initial stage. Then reduce the increment, the increment is 1, in fact, in most cases you can simply fine-tuning will not involve too much data to move.

Hill sorting step substantially under the point of view, this incremental selection gap = length / 2, so as to continue to reduce the incremental gap = gap / 2, which increments by a selected sequence can be represented, {n / 2 , (n / 2) / 2 ... 1}, called incremental sequence. Shell sort selection and prove the incremental sequence is a mathematical problem, choose the increment sequence is more commonly used, and it is proposed increment Hill, Hill called incremental, but in fact this is not optimal incremental sequence of. Examples of use here to do an incremental Hill.

Third, the complexity analysis

1. The time complexity: the worst case, each of the two numbers have a compare and swap, then the time in the worst degree of complexity O (n2), the best case, ordered arrays are not required exchange, only the comparison, the time complexity of best case is O (n).

The large number of people studied, the average time complexity of Shell sort is O (n1.3) (This I do not know how come, the book and the blog have said that, did not find a concrete basis ,,,) .

2. spatial complexity: Hill sorting, only one variable is used for exchanging data with two, regardless of the size of n, the spatial complexity: O (1).

import java.util.Arrays;

public class shell {

public static void main(String[] args) {

int[] arr = new int[]{10,6,3,8,33,27,66,9,7,88};

shellSort(arr);

System.out.println(Arrays.toString(arr));

}

private static void shellSort(int[] arr) {

int temp;

//控制增量序列,增量序列为1的时候为最后一趟

for (int i = arr.length/2; i >0; i/=2) {

//根据增量序列，找到每组比较序列的最后一个数的位置

for (int j = i; j < arr.length; j++) {

//根据该比较序列的最后一个数的位置，依次向前执行插入排序

for (int k = j-i; k >=0; k-=i) {

if(arr[k]>arr[k+i]){

temp = arr[k];

arr[k] = arr[k+i];

arr[k+i] = temp;

}

}

}

}

}

}