[Data structure] Hill sort C language program

void shellSort( int array[], int length)
{
    int i;
     int j;
     int k;
     int gap;     // gap is the step size of the grouping 
    int temp;    // Hill sorting is implemented on the basis of direct insertion sorting, so the sentinel 
    for (gap=length/ 2 is still needed ; gap> 0 ; gap=gap/ 2 ){
         for (i= 0 ; i<gap; i++ ){
             for (j=i+gap; j<length; j=j+gap){ // Single insertion sort 
                if (array[j] < array[j - gap]){
                    temp = array[j];    //哨兵
                    k = j - gap;
                    while(k>=0 && array[k]>temp){
                        array[k + gap] = array[k];
                        k = k - gap;
                    }
                    array[k + gap] = temp;
                }
            }
        }
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324526802&siteId=291194637