Data structure and algorithm--advanced sort: Shell sort and quick sort "to be Continued"

Source: Internet
Author: User

Advanced sorting is much faster than simple sorting, the time complexity of simple sorting is O (n^2), and Hill (shell) sorts O (n (logn) ^2), and quick Sort is O (N*logn).

Note: The following is an example of a small to large sort of an int array.

Hill (shell) sort

Hill sort is based on the insertion sort, first review the insertion sort, assuming that the insert is executed from left to right, the left side of the element to be inserted is ordered, and if the element to be inserted is smaller than the left, you need to nudge all the elements on the left, as shown in:

==>

Figure 1 and Figure 2: Inserting the right side of the temp bar requires the outer mark bit to the left of the five pillars are moved to the right

As shown in 3, the hill sort is done by inserting a fixed interval of elements, then decreasing the interval, and repeating the insertion sort until the interval is reduced to 1.

==>

Figure 3 and Figure 4:outer position and inner-h position of the column do insert sort

The large data size of the graph to see the process more easily visually grasp the algorithm features, 5 and 6, the total number of elements equals 100:

Figure 5 and Figure 6: intervals of 40 and 13 are performed after the insert sort effect

There are two advantages to doing insert sorting at large intervals compared to simple insert sorting:

First, the large interval directly leads to the need to move the data is sparse, and the efficiency of data movement, figure 5 in a single move can span 40 positions;

Second, after the first step of the large interval of the insertion sort, the whole array from the overall point of view has been a clear order, after the small interval of the insertion sort, part of the operation is not necessary to move the data, and again reduce the number of moving data.

Sequence of intervals: Commonly used sequences are Knuth presented by recursive means: h=*3h+1.

The efficiency of hill sequencing: "No one has been able to theoretically analyze the efficiency of hill sequencing, various experiments based on the evaluation of its time-scale from O (n^ (3/2)) to O (n^ (7/6))"--[1].

Quick Sort

(not to be continued ...) )

Reference documents

"1" Java Data structure and algorithm Rober Lafore 2nd

Pictures in the text

The Java Applet Demo applet comes with the article "1".

Data structure and algorithm--advanced sort: Shell sort and quick sort "to be Continued"

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.