The difference between ref and out, bubble sort, general sort, and dichotomy query

Posted by d~l on Sun, 26 May 2019 22:44:42 +0200

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

1. The difference between ref and out:

Out: You need to declare variables before you use them, assign addresses but not assign values, but you need to initialize them when you use them (assign values first when you enter the body of the method). As for why you use them in the body of the method, I personally think it's to distinguish ref; (i.e., only go in and out)

ref: You need to declare and initialize before use, assign addresses and assign values, so that you can bring them in according to the initialized values, and make some logical judgments based on the values you pass in; (i.e., there are in and out, there are heads and tails)

Common: Variables need to be declared first and all have return values.

2. Use method:

First, let's look at how to use both methods: First, we create two methods, one for out and one for 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 out, ref, we can find the difference between the two methods. The parameter count in the out method has initialization value after it enters, and then can be used later. We can use it directly without finding the initialization of count in the ref method body below. So the difference between the two methods is that it is time to call. When a method is called:

 static void Main(string[] args)
            {
                int[] array = new[] { 1223, 918, 234, 765, 974, 867, 86786, 145432, 867633, 9999999 }; // target array
                int findValue = 145432; // Number of lookup
                //Dichotomy results
                Console.WriteLine(BinarySearch(array, findValue, 0, array.Length - 1) ? "The number being looked up exists in an array array in" : "No arrays exist for the lookup number array in");
                //Bubble sort
                int count;
                int[] intlist = bubbleSort(array, out count);
               
                for (int i = 0; i < intlist.Length; i++)
                {
                    Console.Write(intlist[i]+"   ");

                } Console.WriteLine("\t"+"The number of cycles is:"+count);

                //Similar to bubble sorting, the number of single loops is more.
                int count2 = 0;
                int[] intlist2 = bubbleSort2(array, ref count2);
                for (int i = 0; i < intlist2.Length; i++)
                {
                    Console.Write(intlist2[i] + "   ");

                } Console.WriteLine("\t" + "The number of cycles is:" + count2);
                Console.ReadKey();
            }

First look at the dark background color, because before I stuffy in the method body see out in the method body has initialization action, and ref does not, then ref needs to initialize before calling, out does not need to initialize!

Here we are expanding, we know that return can also return value, so what is the difference between return value and the above two?

First of all, we said before, out and ref, as long as we specify out or ref in front of the parameter value, then we can achieve more than one return value, but return can only return a unique value, which is the biggest difference!

And the return value of return is directly non-modifiable, but out and ref can be modified!

 

Second, let's look at the most common sorting algorithms

1. (Ordinary sort) First of all, we borrow the above code.

First, we 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, internal loops are the main loops, if the current value sources[i] is compared with the next value, if the conditions are met, then the value is recorded, which he will cycle all over again, before ending!

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             }

We can see that the above code is the same two loops, but the difference lies in the number of loops in the inner loop. We can see that the number of loops in the inner loop is relative to the number of loops in the outer loop. If the outer loop shows the first data, then the inner loop shows the second number, so the two results are the same, but in the number of loops this (bubble sort) is the same. It's twice as fast as that one, and then we'll execute the following two methods, while looking at the use of out and ref invoked in the above at the same time.

 1 static void Main(string[] args)
 2             {
 3                 int[] array = new[] { 1223, 918, 234, 765, 974, 867, 86786, 145432, 867633, 9999999 }; // target array
 4              
 5                 //Bubble sort
 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"+"The number of cycles is:"+count);
14 
15                 //Similar to bubble sorting, the number of single loops is more.
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" + "The number of cycles is:" + count2);
23                 Console.ReadKey();
24             }

The results are as follows.

 

As a result, we see that this method works, and we also pass the value of the number of cycles we need through out and ref!

Third, let's look at the dichotomy.

Dichotomy: It is commonly understood that finding the required value in a large data is much faster for two people if one person is trying to find it.

Let's look at the code.

 1 static void Main(string[] args)
 2             {
 3                 int[] array = new[] { 1223, 918, 234, 765, 974, 867, 86786, 145432, 867633, 9999999 }; // target array
 4                 int findValue = 145432; // Number of lookup
 5                 //Dichotomy results
 6                 Console.WriteLine(BinarySearch(array, findValue, 0, array.Length - 1) ? "The number being looked up exists in an array array in" : "No arrays exist for the lookup number array in");
 7             }
 8 
 9             /// <summary>
10             /// Binary search/half search (divide and conquer idea, recursion, target array must be ordered sequence), algorithm complexity is o (log (n), n represents the length of the target array)
11             /// </summary>
12             /// <param name="sources">target array </param>
13             /// <param name="findValue">number of target lookups </param>
14             /// <param name="low">interval minimum index </param>
15             /// <param name="high">interval maximum index </param>
16             /// <returns>true: Existence, false, non-existence </returns>
17             private static bool BinarySearch(int[] sources, int findValue, int low, int high)
18             {
19                 // Failure to find, terminate recursion
20                 if (low > high) return false;
21 
22                 // Half-Find Median Index: (a + b) / 2 Represents the arithmetic mean, 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                     // Over the median value, continue searching recursively in the interval [middleIndex + 1, high]
28                     return BinarySearch(sources, findValue, middleIndex + 1, high);
29                 }
30                 if (findValue < sources[middleIndex])
31                 {
32                     // Less than the intermediate value, continue searching recursively in the interval [low, middleIndex - 1]
33                     return BinarySearch(sources, findValue, low, middleIndex - 1);
34                 }
35 
36                 // findValue equals sources[middleIndex], finds, terminates recursion
37                 return true;
38             }

 

This is the way to search by dichotomy.

 

These are just essays. If there are mistakes or points, I hope the gods will not be stingy. Thank you!

Topics: C# less