I see the array and bubble sort

One-dimensional array

Is a fixed-length array of containers, can put the same type of data.
Elements in the array may be any type of data, including basic data types and the reference data type

Professional interpretation

Array (the Array) is a linear table data structure. It uses a set of contiguous memory space to store a set of data having the same type.
Linear table, by definition, the same table is a linear row of data forming a line structure. Data on each linear table most two front and rear directions.
An array of contiguous memory space and having the same data type

Array Declaration

dataType [] arrayName;
NOTE: Array array length can not be specified at the time of declaration, i.e. dataType [n] arrayName; is not legal

Creating arrays

arrayName = new dataType[n];

Dynamic creation (initialization)

dataType[] arrayName = new dataType[n];

Static creation (initialization)

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

An array of memory model

Declare an array that is an array of variable names stored on the stack, create an array of the heap, the heap that is in continuous open space corresponding to the length of the array (all reference types are created on the heap)
I see the array and bubble sort
all reference type variable names stored all addresses.

The initial default value after array creation

Reference type of the array element to null the default initial
character type array elements is a space default initial value (0 corresponding to the characters)
integer type array element default initial value is 0
float type element default initial value is 0.0
Boolean element The initial default value is false

Why array index value from 0 to start?

From the memory array to store the model, the most precise definition of "index" should be "offset (offset)." If a first address of the array is represented, a [0] is the position offset of 0,
i.e. the first address, a [k] represents a position shift on the k type_size, it calculates a [k] of the memory address just use this formula:

a[k]_address = base_address + k * type_size

However, if counted from the array 1, we calculate that array element a [k] becomes the memory address will be:

a [k] _address = base_address + (k-1) * type_size
comparison of the two formulas, we find that, starting from a number of random access array elements each have more than one subtraction, for the CPU, it is more a subtraction instruction. As a very basic array data structure, random access by index array element is its very basic programming operation, optimizing efficiency will achieve the ultimate as far as possible. Therefore, in order to reduce a subtraction operation, the selected array are numbered starting from zero, rather than from the beginning.
There may be historical reasons
C language designers start counting with 0 array subscript, Java, JavaScript and other high-level languages are to follow after the C language, or that C programmers in order to reduce to some extent the cost of learning to learn Java, therefore continue to adopt the habit of counting from zero. In fact, many in an array of languages is also not start counting from 0, such as Matlab. There are even some negative subscript language support, such as Python.

The difference between an array and a linked list

List for insertion and deletion, the time complexity is O (1), the array support random access, according to the random access time index complexity is O (1).

Enhanced for loop through the array

Under standard array in accordance with the order, the right colon sequentially each element of the array assigned to the variable left of the colon, the length of the array for the number of cycles
for (array element type variable names: array name) {
statements;
}

Preparation of a length of 5 integer array, each element is assigned a random integer from 0 to 10, through the array, the output of each element.
int [] = ARR new new int [. 5];
the Random R & lt new new = the Random ();
for (int I = 0; I <arr.length; I ++) {
ARR [I] = r.nextInt (10);
}
for ( T int: ARR) {
System.out.println (T);
}

Multidimensional Arrays

Data type is an array of arrays
I see the array and bubble sort

Dynamic initialization

= New Array Data element type name [rows] [column number];
the number of lines can not be empty

Static initialization

Array type array name [] [] = new types of data [] [] {{element 11, elements 12, ...}, {elements 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;
                        }
                  }
                  //当没有数据交换时,证明数组已排序完成,退出子循环
                  if(!flag){
                        break;
                  }
            }

Two-dimensional array sorting bubble (two ways)

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;

the first method

//二维数组转化为一维数组
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];
            }
}

//对一维数组进行冒泡排序
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;
            }
}
//输出排序后的一维数组
for(int i : array){
            System.out.print(i + " ");
}
System.out.println();
//将排序后的一维数组转化为二维数组
for(int i = 0; i < row; i++){
            for(int j = 0; j < column; j++){
                        arr[i][j] = array[i*column+j];
            }
}
//输出二维数组
for(int i = 0 ; i < arr.length;i++){
            System.out.println(Arrays.toString(arr[i]));
}

The second method


int temp = 0;
            boolean flag = false;
            //所有子数组完成排序需要的冒泡次数总和
            for(int t = 0; t < row * column; t++){    
                  //遍历父数组,即第一维数组(行)的遍历
                  for(int z = 0; z < arr.length; z++){      
                        //每个数组完成排序需要的冒泡次数
                        for(int i = 0; i < arr[0].length; i++){
                              //遍历子数组,并进行冒泡排序
                              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;
                                    }
                              }
                              //当没有数据交换时,证明数组已排序完成,退出子循环
                              if(!flag){
                                    break;
                              }
                        }
                        //输出每组的最大值
                        //System.out.println(arr[z][arr[0].length-1]);
                        //将每组的最大值与下一组的第一个值比较,若大于则交换
                        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;
                        }
                  }
            }
            //输出二维数组
            for(int i = 0 ; i < arr.length;i++){
                  System.out.println(Arrays.toString(arr[i]));
            }

Guess you like

Origin blog.51cto.com/13646338/2448934