Hill sorting of JAVA sorting algorithm

Keywords: shell

Shell Sort

Preface

In this chapter, Hilde's principle and characteristics are not described too much, and the main purpose is to carry out the sorting code of Hilde
 Detailed explanation: most of Baidu's codes are not explained in detail. Here, many comments are added to help readers understand the code, so as to understand
 Understanding the content of hill sorting

brief introduction

Hill sort is a sort of insertion sort, which is also a kind of reduced incremental sort. It is a direct insertion sort algorithm
 A more efficient improved version;
Hill sorting is to group records by a certain increment of subscript, and use direct insertion algorithm to sort each group,
Each group contains more and more elements. When the increment is reduced to 1, the whole array is just divided into one group,
The algorithm will terminate

Sorting process

Reduce increment
Hill sort belongs to insert sort, which divides the whole ordered sequence into bits
 Several small subsequences are inserted and sorted separately;
Sorting process: first, take a positive integer d1 < n, put all array elements with sequence number separated by d1 into a group, and then directly insert them into the group for sorting;
Then take D2 < D1, repeat the above grouping and sorting operations;
Until di=1, i.e. all records are put into a group for sorting;

code implementation

    public static void main(String[] args) { 

        int[] a={49,38,65,97,76,13,27,49,78,34,12,64,1};   //Array before sorting
        int d = a.length;    //Receiving array length with d
        while (true) {       //The condition is always true, keep cycling, use break to jump out of the cycle
            d = d / 2;       //Judge how many pieces are divided into a group, for example, if d=6, then the sub indexes are 0,6,12, 1,7 and 2,8
                             //When d=3, subscripts 0,3,6,9,12 are a group, subscripts 1,4,7,10 are a group, and subscripts 2,5,8,11 are a group
                            //When d=1, 0,1,2,3,4,5,6,7,8,9,10,11,12 are a group
            for (int i = 0;i < d;i++ ) {      //The first while loop, when d=6, is divided into six groups, all i < 6. The array subscript starts from 0, and the first element subscript in each group is 0,1,2,3,4,5, which is represented by i. each for loop is used to arrange the data in this group
                                              //The second while cycle, d = 3, is divided into three groups. At this time, I < 3, perform three cycles. The first element subscript in each group is 0,1,2 respectively. Use for cycle to arrange the data in this group
                for(int j = i + d;j < a.length;j = j + d) {  //The first while loop is divided into six groups. This for loop finds the second element of each group
                                                             //i+1 is the second element in this set of loops. The reason i+1 is to
                                                            //Ensure that the next element exists when entering the body of the loop
                    int temp = a[j];            //Use temp to keep the value of the element to be inserted. When sorting, use direct insertion sorting. Array elements will move backward, and the next element will be overwritten
                    int k;            //Define an initial value for separation from other variables      
                    for(k = j - d;k >= 0 && temp < a[k];k = k - d ) { 
                    //K > = 0 needs to be in front of & & coincidence, because there is a short circuit phenomenon in & & coincidence. When k < 0, the. a[k] subscript is out of range and needs to be short circuited, so as to ensure normal code execution
                    //Insert sorting directly, compare the data in this group, (move backward and overwrite)
                        a[k + d] = a[k];
                    }
                    a[k + d] = temp;   //for loop, k=k-d. after the execution, the judgment condition is not tenable, and the loop is finished, but the loop body is not executed, so k+d instead of K is required here
                    }

                }
            if(d == 1) {    //When d=1, the element interval is 0, that is to say, all elements are divided into a group, and the sorting is completed. Therefore, the cycle jumps out
                break;
            }
        }
        System.out.println("After sorting:");   //Print known arrays
        for(int i = 0; i < a.length;i++)
        {
            System.out.print(a[i] + "  ");
        }
    }

Summary

The German variables i,j,k in the code are relatively convoluted and need to be understood patiently. This chapter mainly helps the reader understand the code content
 As for the principle, many web pages have detailed descriptions

Posted by ChrisBoden on Tue, 05 May 2020 15:15:09 -0700