Differences between ref and out, Bubble sorting, General sorting, binary query, and ref bipartite Query

Source: Internet
Author: User

Differences between ref and out, Bubble sorting, General sorting, binary query, and ref bipartite Query

1. First, let's talk about the differences between ref and out and how to use them;

1. Differences between ref and out:

Out: You need to declare the variable before use, assign the address but not assign a value, but you need to initialize the variable before use (assign a value before entering the method body ), I personally think that the reason for using the method in the method body is to distinguish the ref; (that is, only the outbound content is not included)

Ref,)

Commonalities: both variables must be declared first, and both have return values.

2. Usage:

First, let's take a look at the usage of the two methods: first, we will create two methods, one with out and one with ref, and return an int value.

    

private static int[] bubbleSort(int[] sources,out int count)            {                int temp; 
          count = 0; for (int i = 0; i < sources.Length; i++) { for (int j = i + 1; j < sources.Length; j++) { if (sources[j] < sources[i]) { temp = sources[j]; sources[j] = sources[i]; sources[i] = temp; } count++; } } return sources; }

 private static int[] bubbleSort2(int[] sources, ref int count)            {                int i, j, temp;                for (j = 0; j < sources.Length; j++)                {                    for (i = 0; i < sources.Length - 1; i++)                    {                        if (sources[i] > sources[i + 1])                        {                            temp = sources[i];                            sources[i] = sources[i + 1];                            sources[i + 1] = temp;                        }                        count++;                    }                }                return sources;            }

 

The yellow mark is the out and ref used. The difference can be found in the two methods bodies. The count parameter in the out method has an initialization value after entering, then we can use it later, but we can use it directly without finding the count initialization in the ref method body below. The difference between the two is the call time, the following figure shows how to call a method:

Static void Main (string [] args) {int [] array = new [] {1223,918,234,765,974,867,867 86, 145432,867 633, 9999999}; // destination array int findValue = 145432; // Number of searched items // The result Console of the binary method. writeLine (BinarySearch (array, findValue, 0, array. length-1 )? "The number to be searched exists in the array": "The number to be searched does not exist in the array"); // Bubble Sorting int count; int [] intlist = bubbleSort (array, out count); for (int I = 0; I <intlist. length; I ++) {Console. write (intlist [I] + "");} Console. writeLine ("\ t" + "cycles:" + count); // similar to Bubble sorting, int count2 = 0; int [] intlist2 = bubbleSort2 (array, ref count2); for (int I = 0; I <intlist2.Length; I ++) {Console. write (intlist2 [I] + "");} Console. writeLine ("\ t" + "cycles:" + count2); Console. readKey ();}

First, I only look at the places with dark background colors, because before I stuffy in the method body, I saw that out already had initialization actions in the method body, but ref didn't, then, before calling the ref, You need to initialize it first, and out does not need to be initialized!

Here we are extending it. We know that return can also return values. What is the difference between return values and the above two?

First of all, we have mentioned that as long as the out or ref values are specified before the parameter values, we can implement multiple return values, but with return, we can only return one unique value, this is the biggest difference!

In addition, the return value cannot be modified directly, but the out and ref values can be modified!

 

Ii. Next let's look at the most common sorting algorithms.

1. (Normal sorting) First, we use the above Code,

First, create an array and call the method we have written,

/// <Summary> /// non-Bubble Sorting /// </summary> /// <param name = "sources"> target array </param> /// <param name = "count"> Number of cycles </param> // <returns> ascending order result </returns> private static int [] bubbleSort2 (int [] sources, ref int count) {int I, j, temp; for (j = 0; j <sources. length; j ++) {for (I = 0; I <sources. length-1; I ++) {if (sources [I] <sources [I + 1]) {temp = sources [I]; sources [I] = sources [I + 1]; sources [I + 1] = temp;} count ++ ;}} return sources ;}

This code has internal and external loops. External loops increase the number of loops, and inner loops are the main loops. If the current value sources [I] is compared with the next value, if the conditions are met, record the value. In this case, all the values are cyclically completed!

2. (Bubble Sorting)

1 /// <summary> 2 // bubble sort 3 /// </summary> 4 /// <param name = "sources"> target array </param> 5 /// <param name = "count"> Number of cycles </param> 6 // <returns> ascending order result </returns> 7 private static int [] bubbleSort (int [] sources, out int count) 8 {9 int temp; count = 0; 10 for (int I = 0; I <sources. length; I ++) 11 {12 for (int j = I + 1; j <sources. length; j ++) 13 {14 if (sources [j]> sources [I]) 15 {16 temp = sources [j]; 17 sources [j] = sources [I]; 18 sources [I] = temp; 19} 20 count ++; 21} 22} 23 return sources; 24}

The above Code shows that there are two loops, but the difference is that in the internal loop, we can see the number of loops in the internal loop, which is relative to the number of external loops, if the External Loop shows the first data, the inner loop shows the second number, so the two are the same in the result, but in the number of cycles (Bubble Sorting) this is twice faster than that. Next, we will execute the following two methods. At the same time, we will also use the out and ref methods called in the above section.

1 static void Main (string [] args) 2 {3 int [] array = new [] {1223,918,234,765,974,867,867 86, 145432,867 633, 9999999 }; // target array 4 5 // Bubble Sorting 6 int count; 7 int [] intlist = bubbleSort (array, out count); 8 9 for (int I = 0; I <intlist. length; I ++) 10 {11 Console. write (intlist [I] + ""); 12 13} Console. writeLine ("\ t" + "cycles:" + count); 14 15 // similar to Bubble sorting, 16 int count2 = 0; 17 int [] intlist2 = bubbleSort2 (array, ref count2); 18 for (int I = 0; I <intlist2.Length; I ++) 19 {20 Console. write (intlist2 [I] + ""); 21 22} Console. writeLine ("\ t" + "cycles:" + count2); 23 Console. readKey (); 24}

The running result is as follows:

 

As a result, we can see that this method is run, and the value of the number of loops we need is also passed out through the out and ref!

3. Let's look at the bipartite method.

Bipartite: it is commonly understood to search for the desired value in a big data. If a person wants to search for the required value, it will be much faster for two people,

Let's look at the code.

1 static void Main (string [] args) 2 {3 int [] array = new [] {1223,918,234,765,974,867,867 86, 145432,867 633, 9999999 }; // target array 4 int findValue = 145432; // Number of searched results 5 // result 6 Console. writeLine (BinarySearch (array, findValue, 0, array. length-1 )? "The number to be searched exists in the array": "The number to be searched does not exist in the array "); 7} 8 9 // <summary> 10 // Binary Search/half-fold search (divide the rule, recursion, the target array must be an ordered sequence ), the algorithm complexity is o (log (n), and n represents the length of the target array) 11 /// </summary> 12 // <param name = "sources"> target array </param> 13 /// <param name = "findValue"> target search count </param> 14 /// <param name = "low"> interval minimum index </param> 15 /// <param name = "high"> Interval Maximum index </param> 16 // <returns> true: yes, false, no </returns> 17 private static bool BinarySearch (int [] sources, in T findValue, int low, int high) 18 {19 // not found, end recursion 20 if (low> high) return false; 21 22 // half-lookup median index: (a + B)/2 indicates the arithmetic average, that is, the midpoint 23 int middleIndex = (low + high) % 2 = 0? (Low + high)/2: (low + high)/2 + 1; 24 25 if (findValue> sources [middleIndex]) 26 {27 // greater than the center value, in the interval [middleIndex + 1, high], recursively query 28 return BinarySearch (sources, findValue, middleIndex + 1, high); 29} 30 if (findValue <sources [middleIndex]) 31 {32 // less than the intermediate value. In the range [low, middleIndex-1], recursively continue to search for 33 return BinarySearch (sources, findValue, low, middleIndex-1 ); 34} 35 36 // findValue equals sources [middleIndex], locate, terminate recursion 37 return true; 38}

 

This is the method of searching through the binary method.

 

The above is just a casual article. If there is a mistake or a point, I hope you will not be stingy. Thank you!

 

Related Article

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.