数据结构与算法-排序-(插入排序&希尔排序)


插入排序和希尔排序

1.插入排序

代码如下(示例):

在这里插入图片描述
在这里插入图片描述

package Sort.Insertsort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class Insertsort {
    
    
    public static void main(String[] args) {
    
    
        int[] array = {
    
    101, 34, 119, 1};
        int[] array1=new int[80000];
        for (int i=0;i<80000;i++){
    
    
            array1[i]=(int)(Math.random()*80000);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(new Date()));
        insertsort(array);
        System.out.println(simpleDateFormat.format(new Date()));
        System.out.println(Arrays.toString(array));
    }

    public static void insertsort(int[] array) {
    
    
        for (int i = 1; i < array.length; i++) {
    
    
            int value = array[i]; //要插入的数
            int index = i - 1; //和前一个数比较
            while (index >= 0 && value < array[index]) {
    
    
                array[index + 1] = array[index];
                index--;
            }
            if (index + 1 != i) {
    
    
                array[index + 1] = value;
            }
        }
    }
}

2.希尔排序(2种)

代码如下(示例):
在这里插入图片描述

在这里插入图片描述

package Sort.Shellsort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class Shellsort {
    
    
    public static void main(String[] args) {
    
    
        //int[] array = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
        int[] array = {
    
    101, 34, 119, 1};
        int[] array1=new int[80000];
        for (int i=0;i<80000;i++){
    
    
            array1[i]=(int)(Math.random()*80000);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(new Date()));
        shellsort2(array);
        System.out.println(simpleDateFormat.format(new Date()));

        //System.out.println(Arrays.toString(array));
    }

    //分组加冒泡 = 希尔1排序
    public static void shellsort(int[] array) {
    
    
        int temp = 0;
        for (int gap = array.length / 2; gap > 0; gap = gap / 2) {
    
    
            for (int i = gap; i < array.length; i++) {
    
    
                for (int j = i - gap; j >= 0; j = j - gap) {
    
    
                    if (array[j] > array[j + gap]) {
    
    
                        temp = array[j];
                        array[j] = array[j + gap];
                        array[j + gap] = temp;
                    }
                }
            }
        }
    }

    public static void shellsort2(int[] array) {
    
    
        //分组加插入 = 希尔2排序
        for (int gap = array.length / 2; gap > 0; gap = gap / 2) {
    
    
            for (int i = gap; i < array.length; i++) {
    
    
                int value = array[i];
                int j = i;
                if (array[j] < array[j - gap]) {
    
    
                    while (j - gap >= 0 && value < array[j - gap]){
    
    
                        array[j]=array[j-gap];
                        j = j-gap;
                    }
                    array[j]=value;
                }
            }
        }
    }
}

希尔排序真的烧脑,特别是第二种:分组+插入


总结

有点绕,直接写出来有点困难,需要分步骤一步一步来。后面的分组+插入排序有点难

猜你喜欢

转载自blog.csdn.net/slighting1128/article/details/111688410