Summary of Shell-sort incremental sorting algorithm __acmer ' s

Source: Internet
Author: User
1.shell-sortHill sort (Shell sort) is a sort of insertion. is for the improvement of the direct insertion sort algorithm. The method is also called narrowing the incremental order, due to DL. The shell was named after it was introduced in 1959.
We all know that the direct insertion ranking algorithm is relatively inefficient algorithm, but it is the characteristics of the insertion sort algorithm determines that when the data is small, the order of the data in the basic order is often more efficient than some advanced sorting algorithm framework, more quickly before we begin to understand the shell-sort algorithm , let's review the insert sort algorithm here we first give the pseudo code for the insertion Order:
Data-the array wait to sort
n-the count of elements
this pseudo code for ascending order
i=2 to (n-1)
    tem p = data[i]
    j=i-1 while
    j>=0 and temp<data[j]   //Here without equal sign guarantees the stability of the order
        Data[j+1]=data[j]
        j--
    data[j+1]=temp

From the above we can see that the direct insertion of the sorting algorithm in our array is basically orderly complexity is very efficient, but once the mixture of the situation we are very time-consuming, basic in O (n^2) Time complexity level we note that, The reason why our direct insertion sort algorithm is inefficient is that every time we find our place with the insertion point, we all need to traverse the path of this location, roughly the number of times we exchange becomes very verbose. So, the smart shell thought of an unstable efficient sorting algorithm based on the direct insertion sort algorithm, The famous Shell-sort. the principle of 2.shell-sortLet's start with the mechanics of our optimization, our optimization for direct insertion sequencing is based on the fact that the insertion efficiency is very efficient in the basic order, so the core of our optimization comes to the surface. We continue to make the order of the array of rows up, so that the order of the array gradually increased, Each time we optimize, the array becomes more and more orderly, and the next time the insertion sort becomes more efficient. Here, the witty scientist invents the idea of shell-sort, or incremental sort, to deal with the problem. We choose one increment at a time, To ensure that an incremental range of arrays is ordered, we keep narrowing our increments until 1, which is the process of our last increment bit 1 is equivalent to a direct insert sort, but the previous operation has ensured that our array is already very orderly, The time complexity of our final direct insertion sequencing process has also become very efficient.
Each shell-sort takes several trips, each of which is equivalent to a direct insertion of a fault, as shown in the figure
the choice of 3.shell-pathNow the facts have proved that the core of hill sort is our incremental selection, and our incremental selection requirements end up with an increment of 1, but different selections lead to the difference in the efficiency of our hill sort. Practice has proved that there are a lot of very good step selection options Yes our hill sort on the small scale of the sort time efficiency even more than the quick sort

The choice of step size is an important part of Hill's ranking. As long as the final step size is 1, any step sequence can work.

The algorithm is first sorted in a certain step. It then continues to be sorted in a certain step, and the final algorithm is sorted by step 1. When the step is 1 o'clock, the algorithm becomes the insertion order, which guarantees that the data will be sorted.
The Donald Shell initially recommended that the step size be selected as N/2 and that the step length be half up to 1. While this can be better than an O (N2) class algorithm (insert sort), there is still room to reduce the average time and worst time. Perhaps the most important thing about hill sorting is that when sorted with smaller step size, the larger step size used previously is still in order. For example, if a sequence is sorted by step 5 and then sorted by step 3, the sequence is not only ordered in step 3, but also in step 5. If this is not the case, then the algorithm in the iterative process will disrupt the previous order, then

The sorting will not be completed in such a short time.

Step sequence

The worst case of complexity

(3** (T-k)-1)/2

Fever

The best known step sequence is presented by Sedgewick (1, 5,, 109, the,...), and the sequence's entries come from

9*4^i-9*2^i+1 or 4^i-3*2^i+1 these two formulas.

The study also showed that "comparisons are the main operations in Hill sorting, not the exchange." "The hill sort with such a step sequence is faster than the insertion sort and heap ordering, even faster than the quick sort in the decimal group, but the hill sort is slower than the fast sort when it comes to large amounts of data."

Hill sorted the pseudocode as follows:

Data-array wait to sort
n-the count of the array
dlta-the array Save the Dlta_path for
path=0 to Length_ Dlta for
    I=path to n   //one-segment Direct insertion sort
        temp = data[i]
        j=i-path while j>=0 and
        Temp<data[j]
            DATA[J+PATH]=DATA[J]
            J-=path
        data[j+path]=temp
3.Code of Python
From random import* from time import* to Math import* def shell_sort_0 (data,n): Time=clock () path=int (N/2); While Path:i=path while I<n:temp=data[i] J=i-path while j>=0
            and Temp<data[j]: data[j+path]=data[j] J-=path data[j+path]=temp I+=1 Path=int (PATH/2) return clock ()-time def init_data (data,n): For I in range (n): Data.append (Randint (1,100000)) def Make_dlta (dlta,n): t= (int) (log (2*n+1,3)) for I in range (T): Dlta.append ( int) (0.5* (3** (t-i)-1)) def shell_sort_1 (dlta,data,n): Time=clock () for path in Dlta:i=path Whil E I<n:temp=data[i] J=i-path while J>=0 and temp<data[j]: Dat
A[J+PATH]=DATA[J] J-=path data[j+path]=temp i+=1 return clock ()-time data=[]
N=eval (Input ("Please enter data quantity:")Init_data (data,n) p=[] dlta=[] p=[i for i in data print ("-----------") print ("Time:%lf"% (Shell_sort_0 (p,n))) P=[i for I
In data] Make_dlta (dlta,n) print ('-----------") print (" Time:%lf "% (shell_sort_1 (dlta,p,n)) Time=clock () P.sort ()
 Print ("TIME:%LF"% (clock ()-time))
By experimental data, we found that our table's third kind of hill-sorting efficiency is better than our first two-point 鞥. In fact, the optimal solution of our Hill sort's step selection is actually still a mathematical problem, and we're just finding the best solution at this point, But that's not to say that we found the optimal step selection scheme.
In fact, because of the problem of the constant factor of hill sort, our hill sort is even better than our fast sort in small and medium scale, but once the volume of data increases, there is nothing we can do, as long as the efficiency of O (N*LOGN) algorithm is used to optimize
Experts suggest that any sort of unstable task can be sorted by the hill-first strategy, and once we find that the problem can be optimized again, we switch to a more efficient sort algorithm.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.