hi~I'm here again~
Array 2-Bubble Sorting
1. Name Sources of Reference Data Types [Difficulties]
Basic data types: [Stored in the stack, data threads in the stack do not share independent]
- Numerical type
- Integer type: byte short int long
- Floating point type: float double
- Non-numerical type: char boolean
Reference Data Type: [Stored in the heap, data threads shared in the heap]
- Arrays, classes (String is a class), enumerations
Basic data types store data:
Referencing data types to store data:
// Defining variables double score = 20; double num1 = score; num1 = 3; sout(score); // → 20.0 sout(num1); // → 3.0 // Defined array double[] scores = new double[3]; double[] nums = scores; nums[0] = 5; sout(scores[0]); // → 5.0 sout(nums[0]); // → 5.0
2. Arrays Tool Class
Tools: Enhance productivity, help us solve the past more troublesome content
Some common operations of arrays can be easily implemented.
Refer more to the official API documentation: https://docs.oracle.com/javase/8/docs/api/
double[] arr = {5,2,4,3,9}; // Show everything in the array // Traverse for foreach // 1. Show arrays directly as strings String str = Arrays.toString(arr); sout(str); // Or output Sout directly (Arrays. toString (arr)); // 2. Implementing ascending order (from small to large) of arrays (numerical value) Arrays.sort(arr); sout(Arrays.toString(arr));// String display // Descending output for(int i = arr.length - 1; i >= 0; i--) { sout(arr[i]+" "); } // 3. The implementation stores the contents of the arr array in a new array /*double[] newArr = new double[5]; for(int i = 0; i < arr.length; i++) { newArr[i] = arr[i]; }*/ // Implements copying content from a specified array into a new array of specified length double[] newArr = Arrays.copyOf(arr, 6); sout("\n"+Arrays.toString(newArr)); // 4. Find the location of the specified number in arr /*for(int i = 0; i < arr.length; i++) { if(arr[i] == xx) { sout("Location in "+i"; } }*/ // Binary search requires ascending order first int index = Arrays.binarySearch(arr,5); sout(index); // 5. Fill an array with a specified value double[] arr2 = new double[3]; Arrays.fill(arr2, 9); sout(Arrays.toString(arr2)); // 6. Arrays.asList(a) Understanding // 7. Compare the contents of two arrays double[] arr3 = {2,3,4,5,9}; // sout(arr3 == arr); // sout(arr3.equals(arr)); sout(Arrays.equals(arr3, arr));
3. Master Bubble Sorting [Difficulties]
Bubble Sorting An Algorithms to Realize Sorting Function
Tips: Compare adjacent elements, two are smaller and go ahead
(Preliminary definition of an algorithm: An algorithm is the process of solving a problem. We don't need to invent a solution every time.)
Recurrent comparisons need to be considered. (Arrays and loops)
The outer loop controls the number of rows and the inner loop controls the content of each row.
Comparisons of Outer Cycle Control Rounds and Inner Cycle Control Rounds
// Define an array to store raw data int[] arr = {99,77,88,92,30,22,90,78}; // Bubble Sorting: Outer Cycle n-1 Inner Cycle n-1-i // Outer circulation control wheel number rule: 5 data are compared with 4 data, 6 data are compared with 5 data for(int i = 0; i < arr.length-1; i++) { // Comparisons of Inner Cyclic Control in Each Round: Five Data First Round Comparisons, Four Data First Round Comparisons, Five Data First Round Comparisons for(int j = 0; j < arr.length-1-i; j++) { // Cyclic operation: Comparing adjacent elements, making two or two smaller and moving forward if(arr[j] > arr[j+1]) { // Data Exchange with a Temporary Space int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } sout(Arrays.toString(arr));
4. Multidimensional Arrays (Two Dimensions)
Multidimensional arrays: There are no multidimensional arrays in Java's memory model, but they are supported at the syntactic level. (meaning essentially a one-dimensional array)
Requirements: Store the scores of five students in each of the five classes (one-dimensional array)
double[] scores1 = new double[5]; double[] scores2 = new double[5]; double[] scores3 = new double[5]; double[] scores4 = new double[5]; double[] scores5 = new double[5];
Requirements: Store the scores of five students in each of the five classes (two-dimensional array)
double[][] scores = new double[5][5];
// Grammar of Multidimensional Arrays 1. double[][] arr = new double[5][5]; 2. double[][] arr = {{},{},{},{},{}}; 3. double[][] arr = new double[5][]; arr[0] = new double[3]; arr[1] = new double[5];
These are the recent class notes welcome to read!