JAVA array (including bubble sort, insert sort, dichotomy)

1, Array

Draw a series of continuous spaces in memory space (an array is a variable that stores a group of data of the same data type)

Basic elements of array:

1. Identifier: hump naming rule - > array name

2. Array element -- > value

3. Element subscript: starting from 0, each element in the array can be accessed by subscript

4. Element type: all elements in the array have the same data type -- > type (Object [] / / the largest data type)

5. Array length (0 ~ length-1 -- subscript for element) array name. Length (array length is fixed to avoid array overrun)

2, To work with arrays

1. Declare an array (that is, draw a room on one floor in memory space)

int[] a; data type [] array name = new data class [size];

2. Allocate space

/ / array length cannot be specified while declaring and assigning;

a=new int[5];

scores =new - > allocate space int[30];

avgAge=new int[6];

name =new String[30];

3. Assignment

/*In the case of unassigned values, there will be default values,

Integer default 0,

Decimal default value 0.0;

char type default value empty character;

boolean default value is false;

The default value of String is null (nothing)*/

a[ 0 ]=8;

scroe[0]=89;

int[ ]=scros=={60,80,90,70,85};

int sum =0;

double avg;

for(int i=0//;i<=scores.lenth;i++){

    //sores[i]=input.nextDouble();

    sum=sum+scros[i];

}

avg=sum/scores.length;

Set assignment method: (declaration, allocation space and assignment together)!!! Must be in the same statement;

int a[]={1,2,3,4,5} / / in this case, neither new nor specified length is required;

Array out of bounds; as long as it is not in the normal range (runtime exception)

4. Processing data

a[ 0 ]=a[ 0 ]*10;

public class Xue5 {
    public static void main(String[] args) {
        //int a; / / declaration of variable
        //a=1; / / variable assignment and initialization
        //a=2; / / reassignment of variables
        //a + +; / / variable operation;
        //System.out.println(a) ; use of data
        int []arr;//Declaration of array
        arr=new int[5];//Allocated space must specify length
        arr[0]=1;//Assignment to the first element
        //System.out.println(arr[1]); / / if no value is assigned, there will be a default value. The default value for shaping is 0, and the default value for decimal is 0.0; the default value for char type is null; the default value for boolean is false; the default value for String is null (nothing);
        arr[1]=2;//Assign a value to the second element
        arr[2]=3;
        arr[3]=4;
        arr[4]=5;
        //arr[5]=6; / / array length out of range
        System.out.println(arr[0]);
    }
}

3, Insert new data into the array. The sorting is required to be the same

1. Save the scores in the array;

2. Find the insertion position by comparison

3. This bit element moves one bit back

4. Insert new score

**
 * @Auther Huang qinjie
 * @Date 2020/4/6
 * @Descripion
 * Insert sort
 * From front to back, use a value as the reference value
 * The inner layer starts from the current value, and compares from the back to the front
 * Larger than the current value, move backward, smaller than the current value, and insert the current value after
 * If it has not been inserted, insert it first
 */
public class Charupaixu{
    public static void main(String[] args) {
        int []a=new int[5];
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i <a.length; i++) {
            a[i]=(int)(Math.random()*100);
            System.out.println("The first"+(i+1)+"Results of students:"+a[i]);
        }
        System.out.println(Arrays.toString(a));
        for (int i = 1; i < a.length; i++) {
            int temp=a[i];
            boolean flag=false;
            for (int j = i-1; j>=0; j--) {
                if (a[j]>=temp){
                    a[j+1]=temp;
                    flag=true;
                    break;
                }else{
                    a[j+1]=a[j];
                }
            }
            if (!flag){
                a[0]=temp;
            }
        }
        System.out.println(Arrays.toString(a));
    }
}

4, Use of array (add, delete, modify and query)

1. Add elements

2. Delete element

3. Modify: reassign

4. Queries: traversal

//How to traverse an array

Method 1: int arr[] = new int[3]

             for (int a = 0; a < arr.length; a++) { 

                System.out.println(arr[a]); 

        }

Method 2: for (int a: ARR){ System.out.println (a); }

//Dichotomy search

/**
 * @Auther Huang qinjie
 * @Date 2020/4/6
 * @Descripion
 * Dichotomy search
 * Decompose from the middle to both sides to find the value to be found -- reference value
 * Premise: values are in ascending order
 * 1,Intermediate value found
 * 2,Intermediate value compared with intermediate value
 * a,If the reference value is equal to the middle value, break is found;
 * b,The reference value is larger than the middle value, and it is calibrated as the middle value begin=mid at the beginning;
 * c,The reference value is smaller than the middle value, which is calibrated as the middle value end=mid at the end
 * 3,Repeat the above process can not meet 2.a, then the reference value is not in the array;
 * 4,If there is only one element left in the array
 */
public class Erfenfa {
    public static void main(String[] args) {
        int[]a={1,3,5,7,9,11,13};
        int b=1;
        int end=a.length;//End position of array + 1;
        boolean isFind=false;
        for (int begin = 0; begin <end ;) {//If begin=end, there is no element l in the array;
            int mid=((begin+end)/2);
            if (b==a[mid]){
                isFind=true;
                System.out.println("In the subscript:"+mid+"Location found:");
                break;
            }else if(b>a[mid]){
                begin=mid;
            }else {
                end=mid;
            }
            if (end-begin==1){
                if (b==a[begin]){
                    isFind=true;//If there is only one element left in the array, judge whether it is the number to be searched
                    System.out.println("Under the subscript:0 Location finding;");
                }
                break;
            }
        }if (!isFind){
            System.out.println("non-existent!");
        }
    }
}

5, Array sort:

1. Bubble sorting: double loop, inner loop each cycle, determine a maximum value, and then outer loop control repeated inner loop until the array is in order.

/**
 * @Auther Huang qinjie
 * @Date 2020/4/6
 * @Descripion
 * Bubble ascending
 */
public class MaoPao {
    public static void main(String[] args) {
        int[]a=new int[6];
        for (int i = 0; i < a.length; i++) {
            a[i]=(int)(Math.random()*100);
        }
        System.out.println(Arrays.toString(a));
        for (int i = 0; i <a.length-1 ; i++) {
            for (int j = 0; j <a.length-1-i ; j++) {
               if (a[j]>a[j+1]){
                   int temp=a[j];
                   a[j]=a[j+1];
                   a[j+1]=temp;
               }
            }
        }
        System.out.println(Arrays.toString(a));
    }
}

 //Bubbling descending order
public class MaoPao{
    public static void main(String[] args) {
        int[]a=new int[6];
        for (int i = 0; i < a.length; i++) {
            a[i]=(int)(Math.random()*100);
        }
        System.out.println(Arrays.toString(a));
        for (int i = 0; i < a.length-1; i++) {
            for (int j = 0; j < a.length-1-i; j++) {
                if (a[j]<a[j+1]){
                    int temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        System.out.println(Arrays.toString(a));
    }
}

6, Find the maximum value

Challenge arena: define a variable tmp and compare it with each element of the array. If tmp does not meet the maximum value, reassign the current element to tmp. At the end of the cycle, the maximum value is obtained.

Posted by almightyad on Sun, 21 Jun 2020 19:07:03 -0700