Day_06_Array, Sort

Posted by Walker33 on Fri, 08 Oct 2021 04:31:36 +0200

1. Data structure

One way computers store and organize data, depending on how they store it, has different operating characteristics

Reasonable data structure selection can effectively improve storage efficiency and operational efficiency

Data operations: add delete check

Common data structures;

Array, Chain List, Hash List, Red-Black Tree, B+Tree, Binary Tree, etc...

2. Arrays


Arrays have subscripts specified and are stored continuously, so memory address offsets allow you to quickly locate the data you want to manipulate.
So arrays are very efficient for querying
 
Arrays: slow add-delete, fast query change
  
Variable: Data type variable name = value


  
2.1 Array declaration


2.1.1 Static declaration: Use static declaration when each element in an array is known
Data type [] Variable name = {Value 1, Value 2, Value 3....};
Data type variable name [] = {Value 1, Value 2, Value 3....};
              int i = 1;
int[] a = {1,2,3,4};int1-D array
int[][] b = {{1,2,3},{2,3,4},{4,3,2}};int2-D array
int[][][] c = int 3-D array
     
Arrays can be multidimensional, but the types of each dimension must be consistent
  
2.1.2 Dynamic declaration: Without knowing each element in the array, using dynamic declaration requires specifying the length and then applying the corresponding default value placeholder
Data type [] variable name = new data type [length];
              int[] a = new int[18];
              boolean[] b = new boolean[10];
int[][] c = new int[3][5];There are three one-dimensional arrays in a two-dimensional array, and five data in each one-dimensional array
                      c = {
                              {0,0,0,0,0},
                              {0,0,0,0,0},
                              {0,0,0,0,0},
                          };
              int[][][] d = new int [3][4][5];
  
  
Integer: 0, decimal: 0.0, character: \u0000, Boolean: false, reference type: null

2.1.3 Another way of declaring

int i = 1;
		// Transfer Variables
		m2(i);
		// Pass Literal Quantity
		m2(12);

		int[] arr = {};
		// Transfer Variables
		m1(arr);
		// Pass Literal Quantity
		m1(new int[] { 1, 3, 4 });
	}

	public static void m2(int i) {

	}

	public static void m1(int[] arr) {

	}

2.2 Passing Values and References

Pass value: Pass basic type

Pass-by reference: Pass-by reference type

public static void main(String[] args) {
		int i = 1;
		m1(i);
		// 1 
		System.out.println(i);
		System.out.println("------");
		int[] arr = {1,2,3};
		m2(arr);
		// 100
		System.out.println(arr[0]);
	}
	public static void m1(int i){
		i++;
		// 2
		System.out.println(i);
	}
	public static void m2(int[] arr){
		arr[0] = 100;
		// 100
		System.out.println(arr[0]);
	}

2.3 Array Copy

public static void main(String[] args) {
		int[] src = {2,3,4,5,6,7,8,9};
		int[] dest = {11,12,13,14,15,16,17,18,19};
		// The first parameter is the source array, 2 is the starting position (inclusive), 3 is the target array, 4 is the starting position (inclusive), 5 is the number of substitutions
		System.arraycopy(src, 2, dest, 3, 3);
		for (int i = 0; i < dest.length; i++) {
			System.out.println(dest[i]);
		}
		// Enhanced for-loop foreach
		// For (Data Type Variable: Array){} Type is generally the type of element in the array, and each element in the array is taken out to this variable
		for(int element : dest){
			System.out.println(element);
		}
	}

3.2-D Array

public static void main(String[] args) {
		// Static declaration
		int[][] arr1 = {
				{1,2,3},
				{1,4,6},
				{12,41,11,12,3,45}
		};
		// Dynamic declaration has three one-dimensional arrays, with four elements in each one-dimensional array
		int[][] arr2 = new int[3][4];
		
		// Get the first element
		int[] arr1_0 = arr1[0];
		int arr1_00 = arr1_0[0];
		System.out.println(arr1_00);
		// [1st Dimensional Array][Element]
		System.out.println( arr1[0][0] );
		
		// change
		arr1[0][0] = 100;
		
		arr2[1][2] = 1;
		// ergodic
		for (int i = 0; i < arr2.length; i++) {
			// int[] arr = arr2[i];
			for (int j = 0; j < arr2[i].length; j++) {
				System.out.print(arr2[i][j] + "  ");
			}
			System.out.println();
		}
	}

4.sort

4.1 Exchange variable values

public static void main(String[] args) {
		int x = 2;
		int y = 3;

		// 1 Intermediate variable (commonly used for development)
		// int temp = x;
		// x = y;
		// y = temp;

		// 2 Displacement Operational Exchange (Interview)
		// ^: Convert to binary, XOR for each bit, same 0, different 1
		// 2 0000 0010
		// 3 0000 0011
		// x = x ^ y; // x = 0000 0001
		// y = x ^ y; // y = 0000 0010
		// x = x ^ y; // x = 0000 0011

		// 3 Addition and Subtraction
		x = x + y; // x=5
		y = x - y; // y = 2
		x = x - y; // x = 3

		System.out.println("x : " + x + " , y : " + y);
	}

4.2 Bubble Sorting

1. Compare the two adjacent elements and if the first one is larger than the second, swap positions

2. Do the same work for each pair of adjacent elements. After a round of comparison, the last one must be the largest one

3. Repeat this step for all but the last one

4. Until no pair of elements need to be compared, terminate

public class Array_02_BubbleSort {
	public static void main(String[] args) {
		int[] arr = {3,2,4,5,1};
		bubbleSort(arr);
		for (int i : arr) {
			System.out.println(i);
		}
	}
	public static void bubbleSort(int[] arr){
		for (int i = 0; i < arr.length-1; i++) {
			for (int j = 0; j < arr.length-1-i; j++) {
				if (arr[j] > arr[j+1]) {
					int temp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = temp;
				}
			}
		}
	}
}

4.3 Select Sort

Select Sort:
1. Put the smallest to the left every time
Take out the first hypothesis that it is the smallest, and then compare it one by one with all that follows. If it is smaller than him, save the corresponding subscript 2. When the comparison is complete, save the smallest subscript, and then decide if it is the same as the first one, then transpose if it is different

public class Array_03_SelectSort {
	public static void main(String[] args) {
		int[] arr = {3,2,4,5,1};
		selectSort(arr);
		for (int i : arr) {
			System.out.println(i);
		}
	}
	public static void selectSort(int[] arr){
		for (int i = 0; i < arr.length; i++) {
			// Assume that the current bit is the smallest
			int min = i;
			for (int j = i+1; j < arr.length; j++) {
				// If it is smaller than min, assign the subscript to min
				if (arr[min] > arr[j]) {
					min = j;
				}
			}
			// Determine consistency, and if consistency means i is the smallest, continue with the next cycle
			// If inconsistency is less than i, transpose
			if (min != i) {
				int temp = arr[i];
				arr[i] = arr[min];
				arr[min] = temp;
			}
		}
	}
}

4.4 API Sorting

public class Array_04_API {
	public static void main(String[] args) {
		int[] arr = { 3, 2, 4, 5, 1 };
		// Call directly
		Arrays.sort(arr);
		for (int i : arr) {
			System.out.println(i);
		}
	}
}

Topics: Java