Array simple basic algorithm
1. Creation of array and element assignment (if you know, there may be)
Yang Hui triangle (two-dimensional array), loop number (binary array), 6 numbers, randomly generated between 1-30 and not repeated
2. For numeric arrays
Maximum, minimum, sum, average, etc
/*Find the maximum, minimum, average, sum, etc. of elements in a numeric array Define a one-dimensional array of int type, containing 10 elements and assigning some random integers respectively Then calculate the maximum, minimum, average and sum of all elements and output them All random numbers are required to be two digits [10,99] Formula: (int)(Math.random() * (99 - 10 + 1) + 10) */ public class ArrayTest1{ public static void main(String[] args){ int[] arr = new int[10]; for(int i = 0;i < arr.length; i++){ arr[i] = (int)(Math.random() * (99 - 10 + 1) + 10); } //ergodic for(int i = 0;i < arr.length;i++){ System.out.print(arr[i] + "\t"); } System.out.println(); //Find the maximum value of array elements int maxValue = arr[0]; for(int i = 1;i < arr.length;i++){ if(maxValue < arr[i]){ maxValue = arr[i]; } } System.out.println("The maximum value is:" + maxValue); //Find the minimum value of array elements int minValue = arr[0]; for(int i = 1;i < arr.length;i++){ if(minValue > arr[i]){ minValue = arr[i]; } } System.out.println("The minimum value is:" + minValue); //Sum array elements int sum = 0; for(int i = 0;i < arr.length;i++){ sum += arr[i]; } System.out.println("The sum is:" + sum); //Find the average number of array elements int avgValue = sum / arr.length; System.out.println("The average is:" + avgValue); } }
3. Assignment and copy of array
int[] array1,array2;
array1 = new int[]{1,2,3,4};
package sgg; /*Use simple arrays * Create a class named ArrayExer2 and declare array1 and array2 variables in the main() method. They are typical arrays of int [] * Using braces {}, initialize array1 to eight primes: 2, 3, 5, 7, 11, 13, 17, 19 * Display the contents of array1 * Assign the array2 variable equal to array1, modify the even index element in array2 to make it equal to the index value (for example, array[0] = 0,array[2] = 2), and print out array1, * Think about the relationship between array1 and array2? array1 The same address value as array2 points to a unique array entity in heap space. * Expand and modify the title to realize the replication of array2 to array1 array * */ public class sgg{ public static void main(String[] args) { int[] array1,array2; array1 = new int[] {2,3,5,7,11,13,17,19}; //Display the contents of array1 for(int i = 0;i < array1.length;i++) { System.out.print(array1[i] + "\t"); } //Assign the array2 variable equal to array1 //Cannot assign a value to an array //array2 = array1; //Copy of array: array2 = new int[array1.length]; for(int i = 0;i < array2.length;i++){ array2[i] = array1[i]; } //Modify the even index element in array2 to make it equal to the index value (for example, array[0] = 0,array[2] = 2) for(int i = 0;i < array2.length;i++) { if(i % 2 == 0) { array2[i] = i; } } System.out.println(); //Print out array1 for(int i = 0;i < array1.length;i++) { System.out.print(array1[i] + "\t"); } } }
Assignment:
array2 = array1;
How to understand: the address value of the array saved by array1 is assigned to array2, so that array1 and array2 point to the same array entity in the heap space.
Copy:
array2 = new int[array1.length];
for(int i = 0;i < array2.length;i++){
array2[i] = array1[i];
}
How to understand: we open up a new array space for array2 in the heap space by means of new, and assign the element values in array1 array to array2 array one by one.
4. Inversion of array elements
package sgg; /*Array copy, inversion and search (linear search, dichotomy search) */ public class sgg{ public static void main(String[] args) { String[] arr = new String[] {"JJ","DD","MM","BB","GG","AA"}; String[] arr1 = new String[arr.length]; //Copy of array (different from the assignment of array variable: arr1=arr) for(int i = 0;i < arr1.length;i++) { arr1[i] = arr[i]; } //Method 1: for(int i = 0;i < arr.length / 2;i++){ String temp = arr[i]; arr[i] = arr[arr.length - i -1]; arr[arr.length - i -1] = temp; } //Method 2: for(int i = 0,j = arr.length - 1; i < j;i++,j--){ String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } //ergodic for(int i = 0;i < arr.length;i++) { System.out.print(arr[i] + "\t"); } } }
5. Search for specified elements in the array: search and retrieval
Linear search: compare and search data one by one through traversal
Applicability: it has universal applicability.
Dichotomy lookup:
package sgg; /*Array copy, inversion and search (linear search, dichotomy search) */ public class sgg{ public static void main(String[] args) { String[] arr = new String[] {"JJ","DD","MM","BB","GG","AA"}; String[] arr1 = new String[arr.length]; //binary search //The array to be searched must be ordered int[] arr2 = new int[]{-98,-33,2,33,55,66,77,88,111,129,444}; int dest1 = -33; int head = 0;//Initial first index int end = arr2.length - 1;//Initial last index boolean isFlag1 =true; while(head <= end) { int middle = (head + end) / 2; if(dest1 == arr2[middle]) { System.out.println("The specified element was found at:" + middle); isFlag1 = false; break;} else if(arr2[middle] > dest1) { end = middle -1; } else {//arr2[middle]<dest1 head = middle + 1; } } if(isFlag1) { System.out.println("I'm sorry I didn't find it"); } } }
Implementation idea: compare the intermediate value every time and search in half.
Applicability: (premise: the array must be ordered)
6. Array sorting algorithm
Top ten internal sorting algorithms:
Select sort: directly select sort and heap sort
Swap sort: bubble sort, quick sort
Insert sort: direct insert sort, half insert sort, shell sort
Merge sort
Bucket sort
Cardinality sort
understand:
1. Measure the advantages and disadvantages of sorting algorithm:
Time complexity, space complexity, stability
2. Sort by:
Internal sort and external sort (requires assistant disk)
3. Time complexity of different sorting algorithms
4. Handwritten bubble sorting:
package sgg; /*Bubble sorting of arrays (Implementation) */ public class sgg{ public static void main(String[] args) { int[] arr = new int[]{43,32,76,-98,0,64,33,-21,32,99}; //Bubble sorting 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; } } } } }
7. Use of array tool class:
1. Defined in Java Util package
2.Arrays: provides many methods to manipulate arrays
use:
package sgg; import java.util.Arrays; /*java.util.Arrays:The tool class for operating arrays defines many methods for operating arrays */ public class sgg{ public static void main(String[] args) { //1. Boolean equals (int [] A, int [] b) judge whether two arrays are equal int[] arr1 = new int[]{1,2,3,4}; int[] arr2 = new int[]{1,3,2,4}; boolean isEquals = Arrays.equals(arr1,arr2); System.out.println(isEquals); //2.String toString(int[] a) output array information System.out.println(Arrays.toString(arr1)); //3.void fill(int[] a,int val) fills the array with the specified value Arrays.fill(arr1,10); System.out.println(Arrays.toString(arr1)); } }