Array and Bubble Sorting
Bubble Sorting: Let the data in the array from small to big sort, first two to two comparison (I and I + 1 elements for comparison) n (I-1) times.
Sort data in ascending order.
Raw data of 10, 20, 30, 40, 50, 60, and 70
20, 30, 40, 50, 60, 70, 10 first sorted, Compared 6 times
30, 40, 50, 60, 70, 20, 10 second sorting, compared 5 times
The third sorting of 40, 50, 60, 70, 30, 20, 10 is compared with four times.
50, 60, 70, 40, 30, 20, 10 the fourth sorting, compared 3 times
The fifth sorting of 60, 70, 50, 40, 30, 20, and 10 is compared twice.
70, 60, 50, 40, 30, 20, 10, sorting the sixth time, comparing 1 time
N count needs to be compared N-1 times
The number of times of the T-order comparison is n-t.
Write the minor signs from the largest to the smallest <
For (INT I = 0; I <name. Length-1; I ++)
{
For (Int J = 0; j <name. length-1-i; j ++)
{
For comparison, from large to small <number
}
}
int[] score = { 18, 20, 48, 76, 20, 38, 87, 90, 37, 45, 65, 65, 34, 67, 95 }; for (int i = 0; i < score.Length - 1; i++) { for (int j = 0; j < score.Length - 1 - i; j++) { if (score[j] > score[j + 1]) { int temp = score[j]; score[j] = score[j + 1]; score[j + 1] = temp; } } } for (int i = 0; i < score.Length; i++) { Console.WriteLine(score[i]); } Console.ReadKey();
Method. It is generally used to repeat the code and write it as a method.
Syntax for defining methods
Static methods are called static methods.
Public [access modifier] [static] [Return Value Type] [method name] (parameter)
{Method body}
Return can exit the method immediately. The method name must be in upper case and the parameter name must be in lower case.
Note: 1) In general, methods should be defined in classes.
2) If no return value is returned, Vod is generally written.
3) The method must be followed ();
Static void main (string [] ARGs) {showui (); console. readkey ();} public static void showui () {console. writeline ("###############"); console. writeline ("###########"); console. writeline ("################");}
Call method, which is generally a class. Method Name ();
Parameters
Parameters are required to access another method in one method.
For example, convert. toint32 (string)
The string in it is like a parameter.
(INT number) parameter
(30) real parameters
String Conversion
String S = "123 ";
Int A = int. parse (s );
Return A + B;
For example, convert. toint 32 (string );
static void Main(string[] args) { int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int sum = add(a, b); Console.WriteLine(sum); Console.ReadKey(); } public static int add(int a,int b) { return a + b; }
// Determine whether a leap year int year = convert. toint32 (console. readline (); bool result = leapyear (year); If (result) {console. writeline ("");} else {console. writeline ("not a leap year");} console. readkey ();} public static bool leapyear (INT year) {If (Year % 400 = 0 | year % 4 = 0 & year % 100! = 0) {return true;} else {return false ;}}
Method overload. Outref
For example, the console. writeline () method can be used to reload data of different types, such as int, String, double, and char.
Out is generally used in places where the function requires multiple return values and for outgoing values.
Ref can be understood as a two-way poetry, that is, it can be passed in or out.
Static void main (string [] ARGs) {// learn to use out and ref string S = "123"; int re; If (inttryparse (S, out re) {console. writeline ("conversion successful! "+ RE);} else {console. writeline ("Conversion failed");} console. readkey ();} static bool inttryparse (string S, out int result) {result = 0; try {result = convert. toint32 (s); Return true;} catch {return false ;}}