Hill Sort Algorithms

Hill is the sort proposed in 1959 by the DLShell, so called Shell sort. Hill, also known as narrow sort increment sort, is an improved insertion sort.

The basic idea: Hill sorting is based on the following characteristics of insertion sort: the smaller the number of elements to be sorted in the sorting sequence faster; time ordered sequence of elements to be substantially faster sorting order; based on the above ideas to be sorted into a sequence a plurality of sub-sequences respectively insertion sort, and reduce the number of sub-sequence re-insertion sort, the process is repeated until the sequence to be sorted once again only one insertion sort, the sort sequence is completed ordered.

Code:

/// <summary>
/// 希尔排序
/// </summary>
/// <param name="intArray"></param>
/// <param name="length"></param>
public static void ShellSort(int[] intArray, int length)
{
    int gap, i, j, temp;
    for (gap = length / 2; gap >= 1; gap = gap / 3+1)
    {
        for(i=gap;i<length;i++)
        {
            temp=intArray[i];
            for(j=i-gap;j>=0&&intArray[j]>temp;j-=gap)
                intArray[j+gap]=intArray[j];
            intArray[j+gap]=temp;
        }
        if(gap==1)
            break;
    }
}

It should be noted that there are different effects at different intervals gap will choose the sort of efficiency, not easy to determine the optimum spacing gap, interested friends can search for relevant information as a reference.

These are the Shell sort of content.

Reproduced in: https: //my.oschina.net/secyaher/blog/274440

Guess you like

Origin blog.csdn.net/weixin_33877092/article/details/91966709