1 of basic sorting algorithms -- shellsort

Source: Internet
Author: User

The time complexity of hill sorting is between O (N) and O (n2), and the practical performance in sorting large amounts of data exceeds that of heap sorting. Features:AlgorithmSimple but complex analysis. The following is an implementation:

/** A [] is an array to be sorted * N1 is the T array length * Inc [] is the array to indecate the increasement * N2 is the INC array length */template <typename T> void shellsort (t a [], int N1, int Inc [], int N2) {for (INT I = 0; I <N2; ++ I) {for (Int J = inc [I]; j <N1; ++ J) {t tmp = A [J]; int K; For (k = J; k> = inc [I]; k-= inc [I]) {If (TMP <A [k-Inc [I]) A [k] = A [k-Inc [I]; elsebreak;} A [k] = TMP ;}}}


1. Refer to the descending sequence for hill sorting. The 2k-1 (value <n) sequence can be used. Sedgewick sequence is used for large data volumes.

2. Sorting at each interval is actually an insert sorting instead of a Bubble sorting. So we need to traverse from the back to the front.

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.