# Sort algorithm-Hill sort

Source: Internet
Author: User
Tags element groups

Part of the content transferred from-http://blog.csdn.net/morewindows/article/details/6668714

The Hill (Shell) sort is also known as narrowing the incremental sort , which is an insertion sort . It is a powerful version of the direct insertion sorting algorithm .

The method is due to DL. The shell was named after it was introduced in 1959.

The basic idea of the hill sort is:

The records are grouped by step Gap , and the direct insertion sorting method is used to sort each group of records.
As the stride size decreases , the grouped groups contain more and more records, and when the step value decreases to 1 , the entire data becomes a group that forms an ordered set of records, and the sort is completed.

An array of n=10 49, 38, 65, 97, 26, 13, 27, 49, 55, 4 for example

First time gap = 10/2 = 5

49 38 65 97 26 13 27 49 55 4

1 a 1B

2 a 2B

3 A 3B

4 a 4 b

5 a 5B

1A,1B,2A,2B are grouped, the numbers are the same in the same group, the uppercase letters represent the first elements of the group, and each time the data for the same group is inserted directly into the sort. It is divided into five groups (49, 13) (38, 27) (65, 49) (97, 55) (26, 4) so that each group is sorted (13, 49) (27, 38) (49, 65) (55, 97) (4, 26), the same as below.

Second time gap = 5/2 = 2

After sorting

13 27 49 55 4 49 38 65 97 26

1 a 1 b 1C 1D 1E

2 a 2B 2C 2D 2E

The third time gap = 2/2 = 1

4 26 13 27 38 49 49 55 97 65

1 a 1 b 1C 1D 1E 1F 1G 1H 1I 1J

Fourth time gap = 1/2 = 0 Sort Done to get the array:

4 13 26 27 38 49 49 55 65 97

Realize:

`usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_009_ Hill Sort {classProgram {Static voidMain (string[] args) {            int[] data =New int[] { the, -, the, +, -, -,8, -, *, \$, - };            Shellsort (data);  for(inti =0; I < data. Length; i++) {Console.Write (Data[i]+" "); }        }         Public Static voidShellsort (int[] dataarray) {            intGap = dataarray.length/2;  while(gap>=1)            {                //group the elements from gap to scan all groups                 for(inti = gap; i < dataarray.length; i++)                {                    intIvalue =Dataarray[i]; intj = i-Gap; //sorting element groups that are distance gap                     while(j>=0&& dataarray[j]>ivalue) {Dataarray[j+ Gap] =Dataarray[j]; J-=Gap; } dataarray[j+ Gap] =Ivalue; } Gap= Gap/2;//Decrease Increment            }        }    }}`

Sort algorithm-Hill sort

Related Keywords:

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.