2020/07/14-Array---interpolation, deletion, insertion sort, selection sort

2020/07/14-Array-interpolation, deletion, insertion sort, selection sort

1. Add a number to a sequence of sorted arrays, and still maintain the original sorting method

Ideas:

  • (If the starting array is 3 digits and 1 digit needs to be added, then rebuild a 4-digit array,
  • The reason for rebuilding a larger array is that when you first created the array, you didn’t know how many digits to add later.
  • Therefore, only the required number of array lengths are created at the beginning)
  • Idea: Adding elements to the element group needs to increase or decrease the array capacity
  • If you increase the number by 1, you need to redefine an array that is 1 bit larger than before
  • Then use the for loop to assemble the previous number into the new array
  • The last bit of the new array is empty
public class demo4 {
    
    
    public static void main(String[] args) {
    
    
        int[] a = {
    
    99,85,45,56,21};
        int[] b = new int[6];
        for (int i = 0; i < a.length; i++) {
    
    
            b[i] = a[i];//b的最后一位没有赋值,相当于空,默认是0,
                        // 之后往里面添加数字相当于覆盖
        }
        System.out.println(Arrays.toString(b));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数:");
        int num = sc.nextInt();
        boolean isInsert = false;
        for (int i = b.length-2; i >=0; i--) {
    
     //-2是因为b本身大一位,且下标需小一位
            if(num>b[i]){
    
    //从后向前遍历,从大到小排序,将更小的数放进数组b的最后一个空位
                b[i+1]=b[i];//下标后移
            }else {
    
    
                b[i+1]=num;
                isInsert = true;
                break;
            }

        }
            if(isInsert==false){
    
      //原数组所有数都比新增加的数小,故将新增加的数放在首位
                b[0]=num;
            }
        System.out.println(Arrays.toString(b));
    }
}

Two, delete a number from the array

Ideas:

  • Similar to adding a number to the array, re-create a new array with a smaller one
  • Type the subscript of the number you want to delete in the array
  • Use the for loop to put the numbers in the original array into the new array until i= the subscript,
  • contiune skips this cycle.
public class test3 {
    
    
    public static void main(String[] args) {
    
    
        int[] a = {
    
    99,85,80,63,60};
        int[] b = new int[4];
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的下标:");
        int num = sc.nextInt();
        int j = 0;
        for (int i = 0; i < a.length; i++) {
    
    
            if(i==num) continue;
            b[i] = a[i];
            j++;
        }
        System.out.println(Arrays.toString(b));
    }
}

3. Sort a string of given array elements in ascending order using the selection sort method

  • [Basic sorting methods: select sorting, bubble sorting, insertion sorting] [Advanced sorting algorithms: quick sorting, merge sorting]
  • Ideas:
  • The outer loop defines the minimum value of each a[i] value, compares the a[i] value with each subsequent value, and marks the minimum value and the minimum value subscript
  • Assign a[i] defined by the outer loop to the array corresponding to the subscript of the marked minimum value, and then assign the marked minimum value to a[i]
  • This requires the introduction of a variable to store the subscript
  • It should be noted that when defining the variable, it cannot be defined as 1, or a fixed value.
  • Instead, it should be defined as i, because it does not necessarily produce exchanges every time comparison, and it may keep the original position unchanged.
public class demo5 {
    
    
    public static void main(String[] args) {
    
    
        /*
选择排序逻辑:
从前向后对每一个下表对应的元素作为基准值
与后面所有的元素进行比较,找出最小值
与当前下标的元素进行交换
进行下一次循环
 */       int[] a={
    
    7,6,2,5,8,4,12,3};
        //i代表选取的基准位,j代表的在动的
        for(int i=0;i<a.length-1;i++){
    
    
            int min=a[i];
            int tmp=i;
            for (int j = i; j <a.length ; j++) {
    
    
                if(min>a[j]){
    
    
                    min=a[j];
                    tmp=j;
                }
            }
            a[tmp]=a[i];
            a[i]=min;
        }
        System.out.println(Arrays.toString(a));

        }
    }

Four, insertion sort method

  • Idea: The outer loop starts from item 2, and compares it with the previous number each time.
  • For example: a[1]: compare a[0], if a[1] is big, assign a[0] to a[1]
  • a[2]: Compare with 0, 1 subscript array
public class demo6 {
    
    
    public static void main(String[] args) {
    
    
        /*
        逻辑:从第二位开始,向前比较,进行插入逻辑
         */
        boolean isInsert = false;
        int[] a = {
    
    1,3,8,2,5,7};
        for (int i = 1; i < a.length; i++) {
    
    
            int tmp = a[i];
            for (int j = i-1; j >=0 ; j--) {
    
    
               //如果遇到比自己大的,大的值就后移
                // 如果遇到比自己小的,就在后方插入
                //如果一直没有插入,就插入首位
                if(tmp<a[j]){
    
    
                    a[j+1]=a[j];
                }else
                {
    
     a[j+1]=tmp;
                   isInsert = true;
                   break;}
            }
            if (isInsert!=true){
    
    
                a[0]=tmp;
        }

        }
        System.out.println(Arrays.toString(a));
    }
}

Guess you like

Origin blog.csdn.net/qq_42005540/article/details/107349192