Data structure: Common sorting algorithms (1) - Insertion sorting (direct insertion sorting, Hill sorting)

(1) Direct insertion sort
① Principle:

The entire interval is divided into:
1. Ordered interval
2. Unordered interval
Each time the first element of the unordered interval is selected, select the appropriate position to insert in the ordered interval

②Code implementation:
public class insertSort {
    
    
    public static void main(String[] args) {
    
    
        int[] array={
    
    1,5,78,6,4,8,45,12,0,88};
        System.out.println(Arrays.toString(array));
        inSort(array);
        System.out.println(Arrays.toString(array));
    }
   // 直接插入排序,稳定的排序,时间复杂度:O(n^2),空间复杂度:O(1)
    //越有序,越快
    public  static void inSort(int[] array){
    
    
       for(int i=1;i<array.length;i++){
    
    
           int tmp=array[i];
           for(int j=i-1;j>=0;j--){
    
    
               if(array[j]>tmp){
    
    
                   array[j+1]=array[j];
               }else{
    
    
                   break;
               }
               array[j]=tmp;
           }
       }
    }
   }

Run screenshot:Insert picture description here

③Performance analysis

Insert picture description hereStability: stable
Insertion sort, the closer the initial data is to order, the higher the time efficiency.

(2) Hill sort
① Principle:

Hill sorting method is also called reduced increment method . The basic idea of Hill sorting method is: first select an integer, divide all the records in the file to be sorted into groups, group all records with a distance of 1 into the same group, and sort the records in each group. Then, take and repeat the above grouping and sorting work. When it reaches =1, all records are sorted in the unified group.
1. Hill sorting is an optimization of direct insertion sorting.
2. When gap> 1, all are pre-sorted, the purpose is to make the array closer to order. When gap == 1, the array is already close to order, so it will be very fast. In this way, as a whole, an optimized effect can be achieved. After we realize it, we can compare the performance test.
Insert picture description here

②Code implementation:
public class insertSort {
    
    
    public static void main(String[] args) {
    
    
        int[] array={
    
    1,5,78,6,88,4,8,45,12,0};
        System.out.println(Arrays.toString(array));
        shellSort(array);
        System.out.println(Arrays.toString(array));
    }
    //希尔排序(分组的思想),缩小增量排序
    //时间复杂度:O(1.5)~O(n^2),空间复杂度:O(1)  稳定性:不稳定
    public static  void shellSort(int[] array){
    
    
        int[] arr={
    
    5,3,1};
        for(int i=0;i<arr.length;i++){
    
    
            shell(array,arr[i]);
        }
    }
    public static void shell(int[] array,int gap){
    
    
        for(int i=gap;i<array.length;i++){
    
    
            int tmp=array[i];
            for(int j=i-gap;j>=0;j=j-gap){
    
    
                if(array[j]>tmp){
    
    
                    array[j+gap]=array[j];
                }else{
    
    
                    break;
                }
                array[j]=tmp;
            }
        }
    }
}

Run screenshot:
Insert picture description here

③Performance analysis

Insert picture description hereStability: unstable

Guess you like

Origin blog.csdn.net/qq_47364122/article/details/115327754