Shell Sort)

Source: Internet
Author: User

Hill sorting: downgrading incremental sorting is a sort of insert sorting. According to the previous direct insertion time analysis, the time complexity is O (n ^ 2 ), if the records to be sorted are basically ordered, the time complexity can be increased to O (n)

Basic Idea: first, the entire sequence of records to be sorted is divided into several sub-sequences for direct insertion and sorting. When the whole sequence is basically ordered, all the records are inserted and sorted directly at a time.

Features: 1) subsequences are not simple segments, but are separated by an incremental record to form a subsequence.

2) The increment must be a prime number or a prime number, and the last increment must be 1 to complete a complete sorting.

See the following shell Sorting Algorithm:

Int ShellInsert (MergeType * L, int dk) // sever sort {int nCompKey; int j =-1; for (int I = dk; I <L-> len; I ++) {if (L-> elem [I] <L-> elem [I-dk]) {nCompKey = L-> elem [I]; for (j = I-dk; j> = 0; j-= dk) {if (nCompKey> = L-> elem [j]) {break ;} l-> elem [j + dk] = L-> elem [j];} L-> elem [j + dk] = nCompKey ;}} return 0 ;}
Here, the incremental value is 1 instead of 1 when the comparison is directly inserted, but dk. The others are the same, as shown in the following code:

int ShellSort(MergeType* L, int* dlta, int len){if (!L->elem){return -1;}for (int i = 0; i != len; i++){ShellInsert(L, dlta[i]);}return 0;}
Time complexity of the algorithm: When the incremental sequence is dlta [k] = 2 ^ (t-k + 1)-1, the time complexity of the hill sort is O (n ^ 1.5 ), in short, time sorting is more complex than direct insertion sorting.

The test procedure is as follows. For other function definitions, see bubble Algorithm Improvement.

# Define LISTLEN 10 MergeType pList; pList. elem = (int *) malloc (sizeof (int) * LISTLEN); pList. len = LISTLEN; pList. size = LISTLEN; ScanfList (& pList); // Hill sorting int nDlta [] = {5, 3, 1}; ShellSort (& pList, nDlta, ARRLEN (nDlta )); printList (& pList); free (pList. elem); free (pT. elem); pList. elem = NULL; pT. elem = NULL;

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.