Sorting Algorithm 3: Shell Insert sorting
Sorting Algorithm 3: Shell Insert sorting
Basic concepts related to sorting
Sort: Arrange a group of disordered data according to certain rules.
Data Table(Data list): It is a finite set of data objects to be sorted.
Sort code(Key): Generally, a data object has multiple attribute fields, which are composed of multiple data members. One attribute field can be used to differentiate objects and serve as the sorting basis. This field is the sorting code. The attribute field used by each data table as the Sorting Code depends on the specific application requirements.
Category
Inner sorting: Sorting of all data objects stored in memory during sorting;
External sorting: The number of objects in the sorting process is too large, and the objects cannot be stored in the memory at the same time. The sorting process must follow the requirements of the sorting process to move the sorting between internal and external memory. Analysis of the Stability of the Sorting Algorithm
If there are two objects in the Object Sequence R [I] And R [j] , Their sorting codes K [I] = k [j] . If the objects are sorted before and after R [I] And R [j] If the relative position of is not changed, the Sorting Algorithm is stable; otherwise, the Sorting Algorithm is unstable.
The evaluation time overhead of the sorting algorithm.
Data comparison times and data movement times. Algorithm running time cost is generally estimated
AverageFor estimation. For those objects that are greatly affected by the initial arrangement of the Object Sequence and the number of objects
Best and worst casesEstimate the space overhead
Additional storage required for Algorithm Execution.
Basic Idea of Shell insertion and sorting
First, take a value smaller N Integer D1 As the first increment, all records of the file are divided into d1 groups. All records whose distance is a multiple of dl are placed in the same group. First in each groupSort users directly; Then, take the second Increment D2 < nobr=""> Repeat the above grouping and sorting until the obtained Increment <> Dt = 1 (dt <…< nobr=""> That is, all records are placed in the same group for direct insertion sorting. This method is essentially a grouping insertion method. In fact, when <…<> Dt = 1 Insert the sorting directly. However, after direct insertion and sorting by multiple groups, the data tables before the last complete insertion and sorting are almost sorted, therefore, shell sort makes full use of the efficient feature of directly inserting and sorting data tables under almost ordered conditions.
Algorithm C plus implementation
According to the basic idea of the algorithm, the implementation of shell insertion sorting is still relatively easy. Its C ++ implementation is as follows:
#include
#include
using namespace std;void print(int ar[], int sz, int step){ for(int i = 0; i < sz; ++i) { if(((i + 1) % step) != 0) cout << setw(3) << ar[i]; else cout << setw(3) << ar[i] << endl; } cout << endl;}void ShellSort(int a[], int sz){ int i, j; int step, temp; step = sz / 2 ; while(step) { print(a, sz, step); cout << ==> << endl; for (i = step; i < sz; i++) { temp = a[i]; j = i; while (j >= step && a[j - step] > temp) { a[j] = a[j - step]; j = j - step; } a[j] = temp; } print(a, sz, step); cout << current array << endl; print(a, sz, sz); cout << ---------------- << endl; step = step / 2.2; }}int main(void){ int a[] = {13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10}; const size_t sz = sizeof(a)/sizeof(a[0]); cout << Initial array << endl; print(a,sz,sz); cout << ------------- << endl; ShellSort(a,sz); cout << Sorted array << endl; print(a,sz,sz); return 0;}
In the core part, the while loop in the ShellSort layer is used to find the insert position in the corresponding group and exchange the position in advance. The for loop controls the direct insertion and sorting of data tables that are sorted in the order before each group starting from 2nd. The difference with direct insert sorting is that shell Insert sorting once for inserts the number of I in all groups into their respective pre-i-1 ordered tables.
Output:
Initial array 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10------------- 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10==> 13 14 45 27 73 25 39 10 65 23 94 33 82 25 59 94current array 13 14 45 27 73 25 39 10 65 23 94 33 82 25 59 94---------------- 13 14 45 27 73 25 39 10 65 23 94 33 82 25 59 94==> 13 10 25 23 14 33 27 25 45 39 73 59 82 94 65 94current array 13 10 25 23 14 33 27 25 45 39 73 59 82 94 65 94---------------- 13 10 25 23 14 33 27 25 45 39 73 59 82 94 65 94==> 10 13 14 23 25 25 27 33 39 45 59 65 73 82 94 94current array 10 13 14 23 25 25 27 33 39 45 59 65 73 82 94 94----------------Sorted array 10 13 14 23 25 25 27 33 39 45 59 65 73 82 94 94
Algorithm analysis incremental sequence Selection
The execution time of Shell sorting depends on the incremental sequence.
Common Features of a good incremental sequence:
The last increment must be 1. Try to avoid the mutual multiples of values in the sequence (especially adjacent values.
A large number of experiments have been conducted to show the current good results: WHEN n is large, the number of comparisons and moves is about Nl.25 To 1.6nl.25 . Shell sorting has better performance than direct insertion sorting.
The reason why the time performance of hill sorting is better than that of direct insertion sorting:
When the data table's initial state is basically ordered, the number of comparisons and moves required for direct insertion sorting is small. When n values are small, n and N2 The difference is also small, that is, the best time complexity for directly inserting the sort O (n) And worst time complexity 0 (n2) The difference is not big. At the beginning of hill sorting, there was a large increase, a large number of groups, and a small number of records in each group. Therefore, each group was inserted quickly, and later incremental Di The number of groups gradually decreases, and the number of records in each group increases. Di −1 As a distance sorting, the data table is closer to the ordered state, so the new sorting process is faster.
Therefore, the efficiency of hill sorting is much higher than that of direct insertion sorting.Stability
Hill sorting is unstable. The relative order of the two identical keywords before and after sorting may change.
This method is not efficient for large datasets, but it is one of the fastest algorithms for sorting small data tables (less than 1000. In addition, the memory usage is relatively small.