<Sorting algorithm> Shell sort ShellSort

1. The core idea:
Hill is a sort insertion sort, is an improved version of the direct insertion sort, insertion sort they belong to the same class.
This is an unstable sorting algorithm.
Adopt strategies jumping split: the distance of an "incremental" composed of a sequence of records, so as to ensure a direct result of insertion sort in the sub-sequences get it is basically in order rather than local order.
2. Code implementation:

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  
. 4  void PrintArr ( int ARR [], int len);
 . 5  void ShellSort ( int ARR [], int len)
 . 6  {
 . 7      IF (ARR == NULL || len < = 0 )     return ;
 . 8      
. 9      int GAP = len / 2 ;
 10      int I = 0 ; // packets 
. 11      int J; // each set of each element
12 is  
13 is      the while (GAP> = . 1 )
 14      {
 15          // packet 
16          for (I = 0 ; I <GAP; I ++ )
 . 17          {
 18 is              // insertion sort each group 
. 19              for (J = I + GAP; J <len; = J + GAP)
 20 is              {
 21 is                  int Yes = J-GAP; // ordered inside the group is the first 
22 is                  int in Flag = ARR [J]; // unordered first 
23 is                  the while ( In Flag <ARR [Yes] && Yes> = I)
 24                  {
 25                     arr[yes+gap] = arr[yes];
26                     yes = yes-gap;
27                 }
28                 arr[yes+gap] = flag;
29             }
30             //PrintArr(arr,len);
31         }
32         gap = gap/2;
33     }
34     return ;
35 }
36 
37 void PrintArr(int arr[],int len)
38 {
39     for(int i=0;i<len;i++)
40         cout << arr[i] << " ";
41     cout << endl;
42 
43     return ;
44 }
45 
46 int main()
47 {
48     //int arr[10] = {1,0,2,3,4,5,6,7,8,9};
49     int arr[10] = {4,8,6,3,7,2,9,5,0,1};
50     ShellSort(arr,sizeof(arr)/sizeof(arr[0]));
51     PrintArr(arr,sizeof(arr)/sizeof(arr[0]));
52 
53     system("pause");
54     return 0;
55 }

3. Stability Analysis:
one insertion sort is stable, does not change the relative order of the same elements, but, like elements may move in a respective insertion sort, insertion sort different process, its stability will be the last hit mess, Hill sorting is unstable.
4. Complexity Analysis:
first increment is selected and the time complexity of the relationship. Generally taken to be O (n ^ 1.2 ~ n ^ 1.5), the worst case is O (n). Therefore, medium-size scale to perform well, sort the data on a very large scale is not the best choice. But less than (n) algorithm complexity O much faster.
5. Development:
In addition, Hill algorithm in the worst case and the average case efficiency is not much difference, at the same time efficiency of the implementation of quick sort in the worst case scenario will be very poor. Experts advocate that almost any sort of work can be sorted by Hill at the beginning, if proven in actual use, it is not fast enough, then changed so quickly sort more advanced sorting algorithms. Essentially, Hill sorting algorithm is plugged directly into an improved sorting algorithm, reducing the number of its replication, much faster. The reason is that, when the value of n is large items to be moved to sort each trip number is small, but long distance data items. When the n value decreases each pass requires moving data increase, time has been closer to the final position after the sort them. It is the combination of these two cases before the Hill sorting efficiency is much higher than the insertion sort. Performance packet length sequence selected algorithm has a lot Shell. Only specific sequence of records to be sorted can accurately estimate the number of comparisons keyword object movement and number.

Guess you like

Origin www.cnblogs.com/Aaaaaalei0612/p/11308288.html