Three common methods to obtain the extreme value of array in C sharp(#)

To simplify the discussion, here we use a one-dimensional array
Method one: use only the Systemnamespace. command:using System;

using System;

namespace debug
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            double[] array = new double[] {
    
     0, 1, 2, 1, 7, 5, 4 };
            // 先对array进行排序处理,注意 Array.Sort()函数处理后原函数本身顺序发生变化
            // 默认按从小到大顺序排列
            Array.Sort(array);
            // 最小值就是第一个数据
            Console.WriteLine($"The minimum value of array is {array[0]}");
            // 最大值就是最后一个数据,注意索引从0开始,最后一个数据索引为array的长度减1
            Console.WriteLine($"The minimum value of array is {array[array.Length - 1]}");
        }
    }
}
//The minimum value of array is 0
// The minimum value of array is 7

Of course, using this method, we can not only get the minimum value, but also the second smallest value, just change the index value.

Method 2: Use your own custom sorting function

using System;

namespace debug
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            double[] array = new double[] {
    
     0, 1, 2, 1, 7, 5, 4 };
            // 直接调用我们需要的数组获取值
            double minimum = Min(array);
            double maximum = Max(array);
            // 屏幕打印
            Console.WriteLine($"The minimum value of array is {minimum}");
            Console.WriteLine($"The minimum value of array is {maximum}");
        }

        // 定义自己的排序函数,但是要注意类型应该与我们需要使用的数组保持一致
        public static double Max(double[] array)
        {
    
    
            double max = 0;
            foreach (double element in array)
            {
    
    
                if (element > max)
                {
    
    
                    max = element;
                }
            }
            return max;
        }

        public static double Min(double[] array)
        {
    
    
            double min = 100;
            foreach (double element in array)
            {
    
    
                if (element < min)
                {
    
    
                    min = element;
                }
            }
            return min;
        }
    }
}
//The minimum value of array is 0
// The minimum value of array is 7

Of course, this method is not very simple, because we need to define our corresponding types Maxand Minfunctions according to the type of array we want to study , but we can also use generics to solve this problem uniformly, so I won’t discuss too much here. , May be added later.

Method 3: Use System.Linqnamespace, command:using System.Linq;

using System;
using System.Linq;

namespace debug
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            double[] array = new double[] {
    
     0, 1, 2, 1, 7, 5, 4 };
            // 直接调用我们需要的数组获取值
            double minimum = array.Min();
            double maximum = array.Max();
            // 屏幕打印
            Console.WriteLine($"The minimum value of array is {minimum}");
            Console.WriteLine($"The minimum value of array is {maximum}");
        }
    }
}
//The minimum value of array is 0
// The minimum value of array is 7

Of course C#, there are other methods of obtaining extreme values in programming. Here are just three more common methods for obtaining extreme values.

If you find it useful, please raise your hand to give a like and let me recommend it for more people to see~

Guess you like

Origin blog.csdn.net/u011699626/article/details/110879895