I. Overview
Hill sorting (Shell's Sort) insertion sort is also known as a "zoom increment sort" (Diminishing Increment Sort), is directly into the Ranking algorithm more efficient improved version. Hill sorting non-stationary sorting algorithm. The method proposed in 1959 due to DLShell named.
Sort method | Time complexity (average) | Time complexity (worst) | Time complexity (best) | Space complexity | stability | scenes to be used |
---|---|---|---|---|---|---|
Shell sort | O (N * (logN) 2 ) | O (N 2 ) | O (nlogn) | O (n) | Unstable | Medium-scale data |
1.1, the algorithm description
It is first a large data set into several groups (Packet logic), then each team for each insertion sort, this time, insertion sort acts relatively small amount of data (for each group), the insertion efficiency relatively high
It can be seen that he is pressing the marked points at a distance of group 4, i.e. the difference between the index assigned to a group of 4, such as in this example a [0] and A [4] is set, a [ 1] and a [5] is a group of ..., where the difference (distance) is called an incremental
After each insertion sort packets, each packet becomes the orderly (not necessarily ordered overall)
At this point, the entire array of partially ordered change (probably not very high degree of order)
Then reduced to half the previous increment increment: 2, continue packet division, time, number of elements in each group more, but becomes part of the ordered array, the same insertion sort efficiency ratio
Similarly sorted (sort insert) for each packet, each packet so that a respective orderly
Finally, a set half increment increment: 1, the entire array is divided into a set, at this time, the entire array is close to the orderly, efficient high insertion sort
1.2, code implementation
public static void shellSort(int[] a) { //希尔排序 int d = a.length; while (true) { d = d / 2; for (int x = 0; x < d; x++) { for (int i = x + d; i < a.length; i = i + d) { int temp = a[i]; int j; for (j = i - d; j >= 0 && a[j] > temp; j = j - d) { a[j + d] = a[j]; } a[j + d] = temp; } } if (d == 1) { break; } } }
代码地址:地址 中的algorithm-001-sort中 ShellSort
参看地址:https://blog.csdn.net/qq_39207948/article/details/80006224
https://baike.baidu.com/item/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F/3229428?fr=aladdin