Implementation (Java) and explanation of insertion sort (direct insertion sort, half insertion sort, Hill sort)

Implementation (Java) and explanation of insertion sort (direct insertion sort, half insertion sort, Hill sort)

1. Algorithm overview

​ Insertion sorting is a simple and intuitive sorting method. Its basic idea is to insert a record to be sorted into the previously sorted subsequence according to its key size at a time until all records are inserted. Three important sorting algorithms can be derived from the idea of ​​insertion sorting: direct insertion sorting, half-insertion sorting, and Hill sorting

2. Direct insertion sort

	2|58369147
1、将数组分为有序部分和无序部分,如上面,2一个数必然有序,后面一部分无序
2、将后面的数依次插入到前面,即将5拿出来,插入到前面 于是成为。 
	25|8369147 -> 258|369147 -> 2358|69147
3、依次重复上面的1和2,直到所有的全部有序

Time complexity : O(n^2) in the best case,

stable sort

Applicable to : Linear tables with sequential storage and chain storage

Code

    //1、直接插入排序
    public static int[] insertDirectSort(int[] arr){
    
    
        int l=arr.length;//数组长度
        int temp;//中间变量
        int j;
        for(int i=1;i<l;i++){
    
    
            //判断i与i-1这两个数的大小
            if(arr[i]<arr[i-1]){
    
    
                temp=arr[i];//i更小,将i存入中间变量
                for(j=i-1;j>=0&&temp<arr[j];j--){
    
    
                    //将大于arr[i]且小于等于arr[i-1]的部分往后移动
                    arr[j+1]=arr[j];
                }
                arr[j+1]=temp;
            }
        }
        return arr;
    }

3. Half insertion sort

​ The half insertion sort is mainly to optimize the search part of the direct insertion sort, first use the half search to find the place to be inserted, and then move it uniformly.

​Time complexity : O(n^2)

​Stable sorting method

​Applicable to : linear tables for sequential storage only

Code:

    //2、折半插入排序
    public static int[] halfInsertSort(int[] arr){
    
    
        if(arr == null ||arr.length == 0){
    
    //判断数组为空
            return null;
        }
        int n=arr.length;
        int i, j, low, high, mid,tmp;
        for (i = 1; i < n; i++)  //依次将a[2]~a[n]插入到前面已经排好序的列表
        {
    
    
            tmp = arr[i];  //将a[i]暂存到a[0]
            low = 0;
            high = i - 1;
            while (low <= high)
            {
    
    
                mid = low + (high - low) / 2;  //取中间位置(利用low + (high - low) / 2求mid是为了防止整数溢出问题)
                if (arr[mid] > tmp)  //查找左子表
                {
    
    
                    high = mid - 1;
                }
                else  //查找右子表
                {
    
    
                    low = mid + 1;
                }
            }
            for (j = i - 1; j >= high + 1; --j)  //i - 1指待插入元素的前一个元素,即有序列表中所有大于待插入元素的最后一个元素;high + 1指有序列表中所有大于待插入元素的第一个元素
            {
    
    
                arr[j + 1] = arr[j];  //统一后移元素
            }
            arr[high + 1] = tmp;  //插入操作
        }
        return arr;
    }

4. Hill sort

​Basic idea: Hill sorting is to group the sequence by a certain increment of the subscript, and use the direct insertion sorting algorithm to sort each group; as the increment gradually decreases, each group contains more and more keywords, when the increment When it is reduced to 1, the entire sequence is exactly divided into one group, and the algorithm terminates.

​ Insertion sorting can only move backwards one by one, and Hill sorting can move by skipping steps, and small numbers will soon reach the front.

Human words: 1, 1+x, 1+2x, 1+3x ······· a group, and then use direct insertion sorting in the group; the second group is 2, 2+x,······

​ After a round is completed, reduce the size of x until x=1;

Code implementation :

    //3、希尔排序
    public static int[] hillSort(int[] arr){
    
    
        //实现增量的变化
        for(int gap = arr.length / 2; gap > 0; gap /= 2) {
    
    
            for(int i = gap; i < arr.length; i++) {
    
    
                for(int j = i - gap; j >= 0; j -= gap) {
    
    
                    if(arr[j] > arr[j + gap]) {
    
    
                        int temp = arr[j];
                        arr[j] = arr[j + gap];
                        arr[j + gap] = temp;
                    }
                }
            }
        }
        return arr;
    }

Guess you like

Origin blog.csdn.net/qq_46138492/article/details/129288565