C # Hill sorting

Source: Internet
Author: User

Hill sorting (downgrading incremental method)

It is a sort of insertion class, which divides the entire unordered column into several small subsequences for insertion sorting respectively.

Sorting process: first take a positive integer D1 <n, put all the array elements whose serial numbers are separated by D1 into a group, insert them directly into the group for sorting, and then take D2 <d1, repeat the preceding grouping and sorting operations until di = 1, that is, all records are sorted in a group.

Initial: D = 5

49 38 65 97 76 13 27 49*55 04

49 13

| ----------------- |

38 27

| ----------------- |

65 49 *

| ----------------- |

97 55

| ----------------- |

76 04

| ----------------- |

Results

13 27 49*55 04 49 38 65 97 76

D = 3

13 27 49*55 04 49 38 65 97 76

13 55 38 76

| ------------ |

27 04 65

| ------------ |

49*49 97

| ------------ |

Result

13 04 49*38 27 49 55 65 97 76

D = 1

13 04 49*38 27 49 55 65 97 76

| ---- |

Trigger results

04 13 27 38 49*49 55 65 76 97

Bytes --------------------------------------------------------------------------------------------

For example, the C Language Algorithm for sorting 512,908,170,897,275,653,462,154,509,612,677,765,703, and 94

========================================================== ==========

Function: Hill sorting

Input: array name (that is, the first address of the array), number of elements in the array

========================================================== ==========

*/

/*

========================================================== ================

A brief description of the algorithm concept:

In the direct insertion sorting algorithm, insert a number at a time to add only one node to the sequence,

This does not help insert the next number. If the comparison is relatively long distance (called

Incremental), so that when the number moves across multiple elements, a comparison may be eliminated.

Multiple elements are exchanged. D. L. Shell was implemented in the sorting algorithm named by him in 1959.

This idea. The number of groups to be sorted by the algorithm is divided into several groups according to an incremental D.

The subscript of the record is different. D. Sort all the elements in each group, and then use a small increment.

Sort it and then sort it in each group. When the increment is reduced to 1, the entire number to be sorted is divided

A group. The sorting is complete.

The following function is an implementation of a hill sorting algorithm. The first half of the sequence is incremental,

It will be halved each time until the increment is 1.

Hill sorting is unstable.

 

C #: Code

     /// <summary>
/// Dec:Sort Helper
/// Author:david
/// Create Date:2010-05-05
/// </summary>
public class SortHelper
{
/// <summary>
/// Shell Sort
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
public static void ShellSort<T>(T[] array) where T : IComparable
{
int length = array.Length;
for (int h = length / 2; h > 0; h = h / 2)
{ //here is insert sort
for (int i = h; i < length; i++)
{
T temp = array[i];
if (temp.CompareTo(array[i -h]) < 0)
{
for (int j = 0; j < i; j += h)
{
if (temp.CompareTo(array[j]) < 0)
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
}
}
}

Test code:

 

             int[] array = new int[] {23,15,27,90,69,66,158,45,32,1,45,22};
Console.WriteLine("before shell sort");
foreach (int i in array)
{
Console.Write(i+"->");
}
Console.WriteLine();
SortHelper.ShellSort<int>(array);
Console.WriteLine("after shell sort");
foreach (int i in array)
{
Console.Write(i + "->");
}
Console.WriteLine();
Console.Read();

 

 

Running result:

 

 before shell sort
23->15->27->90->69->66->158->45->32->1->45->22->
after shell sort
1->15->22->23->27->32->45->45->66->69->90->158->

 

 

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.