July 9, 2014

Source: Internet
Author: User

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 ;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.