Chapter05
Array, sort, find
Array initialization
dynamic initialization
int[] arrayDemo = new int[6]; double[] arrayDemo2; arrayDemo2 = new double[5];
initiate static
int[] array = {1,2,3};
Attention to details:
- Array is a reference type, and array data is an object
- The index starts at 0
Array assignment mechanism
- By default, the array is passed by reference, and the attached value is the address. The method is passed by reference.
- Different from the assignment of basic types, it is a copy of values, while an array is the transfer of addresses.
int[] arr1 = {1,2,3}; int[] arr2 = arr1; // If you change the value of arr2, the value of arr1 will also change
Array copy
Data space requirements are independent
int[] arr1 = {1,2,3}; //Open up a new data space int[] arr2 = new int[arr1.length]; arr2 = arr1;
Array inversion
int[] arr = {11,22,33,44,55,66,77,88,99}; int len = arr.length; int[] arr2 = new int[len]; for(int i = len - 1, j = 0; i >= 0; i--, j++) { arr2[j] = arr[i]; } arr = arr2; // Let arr point to the arr2 data space, and arr the original data space // If there is no variable reference, it will be treated as garbage and destroyed for(int i = 0; i < len; i++){ System.out.print(arr[i] + " "); }
int[] arr = {11,22,33,44,55,66,77}; int temp = 0; int len = arr.length; for(int i = 0; i < len / 2; i++) { temp = arr[len - 1 - i]; arr[len - 1 - i] = arr[i]; arr[i] = temp; } for(int i = 0; i < len; i++){ System.out.print(arr[i] + " "); }
Array addition
Scanner myScanner = new Scanner(System.in); int[] arr = {1,2,3}; do { int[] arrNew = new int[arr.length + 1]; for(int i = 0; i < arr.length; i++) { arrNew[i] = arr[i]; } // Add the entered addNum to arrNew System.out.println("Please enter an integer to add:"); arrNew[arrNew.length - 1] = myScanner.nextInt(); arr = arrNew; // The data space pointed to by the arr in the original heap space is cancelled for (int i = 0; i < arr.length; i++){ System.out.print(arr[i] + " "); } System.out.println("\n" + "Successfully added. Please enter whether to continue y/n"); char key = myScanner.next().charAt(0); if(key == 'n') { break; } } while(true);
sort
- Internal sort: load the processed data into memory, such as exchange sort, selective sort and insert sort.
- External sorting: if a large amount of data is not stored in memory, sort with the help of external storage, such as merge sorting method and direct merge sorting method.
Bubble Sorting
The five unordered numbers: 24, 69, 80, 57 and 13 are arranged into an ordered sequence from small to large by bubble sorting method.
Idea:
Array: [24,69,80,57,13]
First round sorting: put the largest number in the last position
- First comparison: [24,69,80,57,13], unchanged
- The second comparison: [24,69,80,57,13], unchanged
- Third comparison: [24,69,80,57,13] - > [24,69,57,80,13]
- The fourth comparison: [24,69,57,80,13] - > [24,69,57,13,80]
The second round of sorting: put the second largest number in the penultimate position
- First comparison: [24,69,57,13,80], unchanged
- Second comparison: [24,69,57,13,80] - > [24,57,69,13,80]
- Third comparison: [24,57,69,13,80] - [24,57,13,69,80]
The third round of sorting: put the third largest number in the penultimate position
- First comparison: [24,57,13,69,80], unchanged
- Second comparison: [24,57,13,69,80] - [24,13,57,69,80]
The fourth round of comparison: put the fourth largest number in the penultimate position
- First comparison: [24,13,57,69,80] - > [13,24,57,69,80]
characteristic:
- A total of n elements need to be sorted (n-1) times
- Each round determines the position of a number
- When comparing, the preceding number is greater than the following number, and the exchange is performed
- Each round of comparison is decreasing
public class BubbleSort{ public static void main(String[] args) { int[] arr = {10,5,32,78,1,2,0,1,4,9}; int len = arr.length; int temp = 0; // Temporary variable for(int i = 0; i < len - 1 ; i++){ for(int j = 0; j < len - 1 - i; j++) { // If the preceding number is greater than the following number, swap if(arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } System.out.println("\n The first" + (i+1) + "Round sorting"); for(int k = 0; k < len; k++) System.out.print(arr[k] + " "); } } }
lookup
Sequential search
Programming idea: judge whether it is successful or not. Judge with an identifier. If the value changes, it will be successful
import java.util.Scanner; public class SeqSearch{ public static void main(String[] args) { String[] names = {"Conan","Maolilan","Ash yuanai","Kaito Kuroba","orchard or garden"}; System.out.println("Enter the detective Conan member you want to find:"); Scanner myScanner = new Scanner(System.in); String findName = myScanner.next(); int index = -1; // Identifier used to determine whether it is found for(int i = 0; i < names.length; i++) { if(findName.equals(names[i])) { System.out.println("Congratulations, I found it!" + "Serial number is" + (i + 1)); index = i; break; } } if(index == -1) { System.out.println("Sorry,there is no such name that you want to find."); } } }
Binary lookup (array is ordered)
Two dimensional array
int[][] arr = new int[2][3];// The first is the number of one-dimensional arrays contained in the array, and the second is the size of one-dimensional array
int[][] arr = {{}}; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + " "); } }
Two dimensional array int[2] [3] in memory
be careful:
Elements in a two-dimensional array and a one-dimensional array may not be equal
/* Dynamically create the following array 1 1 2 1 2 3 ...... */ // Dynamically create an array as follows. arr points to a space in the heap, but the space contains three null s int[][] arr = new int[9][]; for (int i = 0; i < arr.length ;i++) { // Give each one-dimensional array space arr[i] = new int[i + 1]; for (int j = 0; j < arr[i].length; j++) { arr[i][j] = j + 1; } }
Declaration method of two-dimensional array: int[] [] arr, int[] arr [], int arr [].
int x,y[];// x. Y is a two-dimensional array x[0] = y;// error y[0] = x;// ok y[0][0] = x;//error x[0][0] = y;//error y[0][0] = x[0];//ok x = y;//error
practice
- Declaration of string array:
String[] strs = new String[]{"yang"};// The second comprehensive number cannot have numbers
-
For an ascending array, insert a number into the array to ensure that the array is still in ascending order
InsertNum.java
public class InsertNum{ public static void main(String[] args) { //For an ascending array, insert a number into the array to ensure that the array is still in ascending order /*Essential array expansion + positioning 1. Determines which index of the array to insert 2. Capacity expansion */ int[] arr = {10, 12, 45, 90}; int insertNum = 16; // The index will be inserted at the position where it is found, and the index value found will be retained int index = -1; /* Idea: first traverse the entire array. If insertnum < = arr [i], then index = i; If the above i is not found after traversal, then index = arr.length. Place the number at the end of the array */ for (int i = 0; i < arr.length; i++) { if (insertNum <= arr[i]) { index = i; break; } } if (index == -1) { index = arr.length; } // Copy the array, and use j to control the elements behind the arr array index to continue to expand after inserting numbers int[] arrNew = new int[arr.length + 1]; for (int i = 0, j = 0; i < arrNew.length; i++) { if (i != index) { arrNew[i] = arr[j]; j++; } else { arrNew[i] = insertNum; } } arr = arrNew; for (int j = 0; j < arr.length; j++) { System.out.print(arr[j] + " "); } System.out.println(); } }