Array and bubble sort in my eyes

One-dimensional array

An array is a fixed-length container that can hold the same type of data.
Elements in an array can be any data type, including basic and reference data types

Professional Interpretation

Array is a linear table data structure.It uses a continuous set of memory space to store a set of data of the same type.
Linear tables, as the name implies, are data rows that are structured like a line.Data on each linear table has at most two directions, front and back.
Arrays have continuous memory space and the same data type

Declaration of array

dataType[] arrayName;
Note: Arrays cannot be declared with an array length, that is, dataType[n] arrayName; it is illegal

Array creation

arrayName = new dataType[n];

Dynamic Creation (Initialization)

dataType[] arrayName = new dataType[n];

Static Creation (Initialization)

dataType[] arrayName = new dataType[]{value1,value2,......,valueN};

Memory model for arrays

The declaration of the array, that is, the name of the array variable, is stored on the stack, and the creation of the array is in the heap, which opens up a continuous space corresponding to the length of the array in the heap (all creation of reference types are in the heap)

All variable names of reference types have addresses.

Initial default after array creation

The default initial value of an array element of a reference type is null
The default initial value of an array element of type character is a space (the character 0 corresponds to)
The default initial value of an array element of integer type is 0
Elements of floating point type have a default initial value of 0.0
The default initial value of an element of Boolean type is false

Why do array index values start at 0?

From the memory model of array storage, the most precise definition of "subscript" should be "offset".If A is used to represent the first address of the array, a[0] is offset to zero.
That is, the first address, a[k] represents the offset of K type_size s, so this formula is all you need to calculate the memory address of a[k]:

a[k]_address = base_address + k * type_size

However, if the array starts counting from 1, the memory address where we compute the array element a[k] becomes:

a[k]_address = base_address + (k-1)*type_size
Comparing the two formulas, it is easy to find that from the beginning of numbering, there is one more subtraction operation for each random access to array elements, which is one more subtraction instruction for CPU.Array is a very basic data structure, and random access to array elements through subscripts is a very basic programming operation, so the optimization of efficiency should be as high as possible.So to reduce one subtraction operation, the array is numbered from 0 instead of from 1.
There may also be historical reasons
C language designers start counting array subscripts with 0, and later advanced languages such as Java and JavaScript follow the C language, or to reduce the cost of learning Java to some extent, C language programmers continue to follow the practice of counting from 0.In fact, arrays are not counted from zero in many languages, such as Matlab.There are even languages that support negative subscripts, such as Python.

Difference between Array and Chain List

Chain lists are suitable for insertion and deletion, with a time complexity of O(1), arrays supporting random access, and random access based on subscripts having an O(1) time complexity.

Enhanced for loop traversal array

In the order of array subscripts, each element in the array to the right of the colon is assigned to the variable to the left of the colon, and the length of the array is the number of for loops
For (array element type variable name: array name){
Sentence;
}

Write an array of integers of length 5, assigning each element a random integer of 0-10, traversing the array, and outputting each element.
int[] arr = new int[5];
Random r = new Random();
for(int i = 0; i < arr.length; i++){
arr[i] = r.nextInt(10);
}
for(int t : arr){
System.out.println(t);
}

Multidimensional Array

Data type is an array of arrays

dynamic initialization

Array name= new data element type [number of rows] [number of columns];
Number of rows cannot be empty

initiate static

Array type array name [][] = new data type [][] {element 11, element 12,...}, {Element 21,...}}

One-dimensional array bubble sort

int[] arr = new int[]{4,45,1,13,89,7};
            int temp;
            for(int i = 0; i < arr.length; i++){
                  for(int j = 0; j < arr.length - 1;j++){
                        if(arr[j] > arr[j+1]){
                              temp = arr[j];
                              arr[j] = arr[j+1];
                              arr[j+1] = temp;
                        }
                  }
            }

optimization

int[] arr = new int[]{4,45,1,13,89,7};
            int temp;
            boolean flag = false;
            for(int i = 0; i < arr.length; i++){
                  for(int j = 0; j < arr.length - i -1;j++){
                        if(arr[j] > arr[j+1]){
                              temp = arr[j];
                              arr[j] = arr[j+1];
                              arr[j+1] = temp;
                              flag = true;
                        }
                  }
                  //When there is no data exchange, prove that the array is sorted and exit the subloop
                  if(!flag){
                        break;
                  }
            }

Two-dimensional array bubble sort (two methods)

Create a two-dimensional array

Random r = new Random();
            int[][] arr = new int[4][6];
            for(int i = 0; i < arr.length; i++){
                  for(int j = 0; j < arr[0].length;j++){
                        arr[i][j] = r.nextInt(100);
                        System.out.print(arr[i][j]+" ");
                  }
                  System.out.println();
            }
            int row = arr.length;
            int column = arr[0].length;

First method

//Converting a two-dimensional array to a one-dimensional array
int[] array = new int[row * column];
for(int i = 0; i < row; i++){
            for(int j = 0; j < column; j++){
                        array[i*column+j] = arr[i][j];
            }
}

//Sort one-dimensional arrays by bubble
int temp;
boolean flag = false;
for(int i = 0; i < array.length; i++){
            for(int j = 0; j < array.length - i -1;j++){
                        if(array[j] > array[j+1]){
                                    temp = array[j];
                                    array[j] = array[j+1];
                                    array[j+1] = temp;
                                    flag = true;
                        }
            }
            if(!flag){
                        break;
            }
}
//Output Sorted One-Dimensional Array
for(int i : array){
            System.out.print(i + " ");
}
System.out.println();
//Converts a sorted one-dimensional array to a two-dimensional array
for(int i = 0; i < row; i++){
            for(int j = 0; j < column; j++){
                        arr[i][j] = array[i*column+j];
            }
}
//Output 2-D Array
for(int i = 0 ; i < arr.length;i++){
            System.out.println(Arrays.toString(arr[i]));
}

Second method

int temp = 0;
            boolean flag = false;
            //Total number of bubbles required for all subarrays to complete sorting
            for(int t = 0; t < row * column; t++){    
                  //Traversal through the parent array, that is, the first dimension of the array (rows)
                  for(int z = 0; z < arr.length; z++){      
                        //Number of bubbles per array needed to complete sorting
                        for(int i = 0; i < arr[0].length; i++){
                              //Traverse subarrays and sort by bubble
                              for(int j = 0; j < arr[0].length - 1 -i;  j++){
                                    if(arr[z][j]>arr[z][j+1]){
                                          temp = arr[z][j];
                                          arr[z][j] = arr[z][j+1];
                                          arr[z][j+1] = temp;
                                          flag = true;
                                    }
                              }
                              //When there is no data exchange, prove that the array is sorted and exit the subloop
                              if(!flag){
                                    break;
                              }
                        }
                        //Output maximum for each group
                        //System.out.println(arr[z][arr[0].length-1]);
                        //Compare the maximum value of each group to the first value of the next group, swap if greater
                        if(z < arr.length - 1 && arr[z][arr[0].length-1]  > arr[z+1][0]){
                              temp = arr[z][arr[0].length-1];
                              arr[z][arr[0].length-1] = arr[z+1][0];
                              arr[z+1][0] = temp;
                        }
                  }
            }
            //Output 2-D Array
            for(int i = 0 ; i < arr.length;i++){
                  System.out.println(Arrays.toString(arr[i]));
            }

Tags: Java C Programming Javascript

Posted on Fri, 08 Nov 2019 20:18:24 -0500 by thedream