Insertion sort of sorting algorithm (direct insertion sort and Hill sort)

The basic idea of ​​insertion sorting is: each time a record to be sorted is inserted into the appropriate position in the previously sorted file according to the size of its key until all records are inserted. Insertion sort mainly includes direct insertion sort and Hill sort. 

direct insertion sort

Direct insertion sorting is a relatively simple sorting method. The basic operation is: there is an area for n elements to be sorted, and it is gradually divided into two parts during the sorting process, one is an ordered area and the other is an unordered area. In the process, you only need to take out the first element from the unordered area each time, and insert it into the appropriate position in the ordered area to make it a new ordered area. After n-1 insertions in this way, the unordered area is empty, the sorted area contains all n elements, and the sorting is complete. The inline algorithm is stable.

Direct insertion sort algorithm implementation:

    void InserSort (int arr[], int n)
    {
        int i,j;
        for(i=1; i<n; i++)
        {
            //将数据插入有序表
            int tmp=arr[i];     //设置哨兵
            if(tmp>arr[i-1])
            {
                //将比当前数据大的元素依次向后移动
                for(j=i-1; j>=0 && tmp>arr[j]; j--)
                    arr[j+1]=arr[j];
                arr[j+1]=tmp;
                
            }
        }
    }

Direct insertion sort animation demonstration:

Hill sort

Hill sorting, also known as "reducing incremental sorting", was proposed by Hill in 1959. The basic idea is: first take an integer d smaller than n as the first increment, divide all elements in the array R into d1 groups, and put all elements whose subscript distance is a multiple of d in the same group, that is, R[ 1], R[1+d1], R[1+2*d1] is the first group, R[2], R[2+d1], R[2+2*d1] is the second group, and so on , then perform direct insertion sorting in each group, and then take d2 (d2<d1) as the second increment, repeat the above grouping and sorting until the increment dt=1, and put all elements in the same group Perform direct insertion sort. Hill sort is unstable.

Hill sorting algorithm implementation:

    void ShellSort (intarr[],int n)
    {
        int i, j, d;
        int tmp;
        d=n/2;  //设置增量初值
        while(d>0)
        {
            for(i=d; i<n;i++)   //对所有相隔d的元素组进行直接插入排序
            {
                tmp=arr[i];
                j=i-d;
                while(j>n>=0 && tmp>arr[j])    //对每组中的数据进行排序
                {
                    arr[j+d]=arr[j];
                    j=j-d;
                }
                arr[j+d]=tmp;
            }
            d=d/2;
        }
    }

First round of sorting:

When d1=5: subscript 1 and subscript 6 in the above figure are a group, subscript 2 and subscript 7 are the same group, and so on, the same color is a group, and the group is directly inserted and sorted, from small to small Large, the order of 55<100 remains unchanged , the order of 22>11 is reversed , the order of 99>33 is reversed , the order of 77>66 is reversed , and the order of 44<88 remains unchanged .


Second round of sorting:

 When d2=3: subscript 1, subscript 4, subscript 7 and subscript 10 in the above figure are a group, and so on, the same color is a group, and the group is directly inserted and sorted, from small to large, the first The reordering of 55, 66, 22, 88 in one group should be 22, 55 , 66, 88; the order of 11, 44 , 99 in the second group remains unchanged; the reordering of 33, 100 , 77 in the third group should be 33, 77, 100 .


The third round of sorting:

 When d3=1: All elements in the above figure are a set of direct insertion sorting.


Guess you like

Origin blog.csdn.net/m0_61843874/article/details/124766615