Shell Insert sorting

Source: Internet
Author: User

Shell Insert sorting
Basic Thinking of hill sorting

Basic Idea:

Take an integer D1 smaller than N as the first increment, and divide all records of the file into D1 groups. All records whose distance is a multiple of DL are placed in the same group. Insert and sort data in each group first. Then, repeat the group and sort data in the second incremental D2 <d1 until the incremental dt = 1 (dt <DT-L <... <D2 <d1), that is, all records are placed in the same group for direct insertion sorting.

This method is essentially a grouping insertion method.

Shell sort is also called "downgrading incremental sorting ". It does not compare one element to another at a time. Instead, the entire sequence of records to be sorted is divided into several sub-sequences for direct insertion and sorting. When the records in the whole sequence are basically ordered, all the records are directly inserted and sorted. This greatly reduces the number of records moved and improves the sorting efficiency.

Algorithm idea: first take a positive integer D1 (d1 <n) and divide all records into D1 groups. All records whose distance is a multiple of DL are considered as a group, insert and sort data in each group. Then, take D2 (D2 <d1) and repeat the preceding grouping and sorting operations until di = 1 (I> = 1 ), that is, all records are in the same group. There is no strict rule on the selection of incremental sequences in Hill sorting. D1 is usually about n/2, D2 is D1/2, D3 is D2/2 ,..., DI = 1.

Algorithm:

Void shellsort (int A [], int N) // R [1], R [2],…, R [N] is the element to be sorted, where a [0] is the auxiliary space,
{
Int K, I, J;
K = n/2;
While (k> = 1) // group sorting, knowing that the last sorting is, when all records are in one group, that is, when k = 1
{
// Do not confuse the conditions here with the array subscript. When n is 10, the total length of the array is 11.
For (I = k + 1; I <= N; I ++) // from k = 1 elements, k elements are separated.
{
A [0] = A [I]; // sort by direct insertion
J = I-K;
While (j> 0 & A [0] <A [J])
{
A [J + k] = A [J];
J = J-K;
}
A [J + k] = A [0];
}
K = K/2;
}
Return;
}

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.