9. C # basic sorting (multi-dimensional array ),
Multi-dimensional array 1 and two-dimensional array:
Representation Method:
Int [y, x], x and y are indexes, y represents rows, and x represents columns.
Example:
Int [,] second = new int [2, 3] {3, 2, 5}, {6, 7, 8}; // {} can be left empty
Modification method:
Second [0, 1] = 3; // indicates that the number in column 0th of row 1st is changed to 3.
Exercise: Use a two-dimensional array for Bubble Sorting:
Enter the number of people, enter the age, height, and name of each person, and calculate the average age, sorted by height from high to low
Console. writeLine ("Enter the number of students:"); int n = int. parse (Console. readLine (); string [,] ren = new string [n, 3]; // enter the information of each student for (int I = 0; I <n; I ++) {Console. writeLine ("Enter name, age, and height, separated by the Enter key:"); for (int j = 0; j <3; j ++) {ren [I, j] = Console. readLine () ;}} double sum = 0; // calculate the total age, print the average age for (int I = 0; I <n; I ++) {sum = sum + int. parse (ren [I, 1]);} Console. writeLine ("Average age: {0}", Math. floor (sum/n); Console. writeLine ("name, age, height"); // sort by height (int I = 0; I <n; I ++) {for (int j = I; j <n; j ++) {if (int. parse (ren [j, 2])> int. parse (ren [I, 2]) {string [] zhong = {ren [j, 0], ren [j, 1], ren [j, 2]}; // exchange all information so that the height order is consistent with the name and age. ren [j, 0] = ren [I, 0]; ren [j, 1] = ren [I, 1]; ren [j, 2] = ren [I, 2]; ren [I, 0] = zhong [0]; ren [I, 1] = zhong [1]; ren [I, 2] = zhong [2] ;}} int [,] AB = new int [0, 0]; for (int I = 0; I <n; I ++) {for (int j = 0; j <3; j ++) {Console. write (ren [I, j] + "");} Console. write ("\ n ");}
* 2. Multi-dimensional array
Description: int [z, y, x]: z indicates several two-dimensional arrays.