Shell sorting algorithm: An improved insertion sort algorithm

Source: Internet
Author: User
Tags sort time interval

The shell sorting algorithm was first proposed by the D.L shell in 1959, assuming that the elements to be sorted have n, then each insertion sort is not the same as the elements, but goes to an interval.

The shell first sets the interval to N/2, then jump to the insertion sort, and then set the interval to N/4, jump to sort the action, and then set the time interval for N/8, N/16, know the interval is 1 after the last sort termination, because the last sort action will be the fixed interval of the elements sorted well , so when the last sort terminates after the interval is 1, because the previous sort action will sort the elements within the fixed interval, the higher the probability that some elements will be in the correct position when the interval is more and more, so the last few sorting actions can be drastically reduced.

For example, if there is an unordered number like right: 89 12 65 97 61 81 27 2 61 98

There are 10 numbers in total, so the first time we set the interval to 10/2=5, we sort the numbers at intervals of 5, as follows:

The part of the summary line that represents the part to be sorted together, and then the quotient that sets the interval to 5/2, which is 2, the second insertion sort object looks like this:

Then the interval is set to 2/2=1, this time is simply the insertion of the sort, because most of the elements have been roughly sorted, so the last time the insertion of the sort of opportunity did not have any sort of action:

Setting the interval to N/2 is a good illustration of the D.L Shell's first proposed use of this interval in textbooks, but the key to shell sorting is the setting of intervals, such as sedgewick proof that selecting the following intervals speeds up the shell sorting algorithm:

where 4* (2j) 2 + 3* (2j) + 1 can not exceed the total number of elements n value, find the first interval for J descendants in 4* (2j) 2 + 3* (2j) + 1 using the upper-style, and then the second interval by dividing 2j by 2, and then by analogy.

It was later proved that there were other intervals chosen to speed up the shell sort algorithm, and the concept of the shell sort algorithm could also be used to improve the bubble 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.