从列表中查找最小值

本文关键字:最小值 查找 列表 | 更新日期: 2023-09-27 18:34:09

我将如何在这里以最有效的方式构建算法以从列表中找到最小值?我知道该列表没有以最好的方式完成,但是,任何想法该怎么做?我已经尝试了几种方法,但似乎没有让它有效地工作。

谢谢。

class MainClass
{
    public class List
    {
        public int maxSize = 50;
        public int MaxSize
        {
            get
            {
                return maxSize;
            }
            set
            {
                maxSize = value;
            }
        }

        public int firstEmpty = 0;
        public int FirstEmpty
        {
            get
            {
                return firstEmpty;
            }
            set
            {
                firstEmpty = value;
            }
        }
        public int[] data;

        public List()
        {
            data = new int[maxSize];
        }

        public int returnValueAtIndex(int i)
        {
            return data[i];
        }

        public void setValueAtIndex(int v, int i)
        {
            data[i] = v;
        }
    }

    public static int FIRST(List L)
    {
        if (END(L) > 0)
            return 0;
        else
            return -1;
    }
    public static int END(List L)
    {
        return L.FirstEmpty;
    }
    public static int NEXT(int p, List L)
    {
        if (p >= 0 && p < L.MaxSize && p < END(L))
            return p+1;
        else
            return - 1;
    }
    public static int PREVIOUS(int p, List L)
    {
        if (p >= 0 && p < L.MaxSize && p <= END(L))
            return p-1;
        else
            return -1;
    }
    public static int LOCATE (int x, List L)
    {
        int i = 0;
        while (i<END(L) && RETRIEVE(i, L) != x)
        {
            i++;
        }
        if (i != END(L))
            return i;
        else
            return -1;
    }
    public static int RETRIEVE(int p, List L)
    {
        if (p >= 0 && p < END(L))
            return L.returnValueAtIndex(p);
        else
            return -1;
    }
    public static void INSERT(int x, int p, List L)
    {
        if (p >= 0 && p < L.MaxSize && p <= END(L))
        {
            if (p == END(L))
            {
                L.setValueAtIndex(x, p);
            }
            else
            {
                for (int i = END(L); i > p; i--)
                {
                    L.setValueAtIndex(L.returnValueAtIndex(i - 1), i);
                    L.setValueAtIndex(x, p);
                }
            }
            L.FirstEmpty = END(L) + 1;
        }
        else
            Console.WriteLine("Alkiota ei voitu lisätä");
    }
    public void DELETE(int p, List L)
    {
        if (p >= 0 && p < END(L))
        {
            for (int i = p; i < p - 1; i++)
            {
                L.setValueAtIndex(L.returnValueAtIndex(i + 1), i);
            }
            L.FirstEmpty = END(L) - 1;
        }
    }
    public void MAKENULL(List L)
    {
        L.FirstEmpty = 0;
    }
    public static void PRINT(List L)
    {
        Console.WriteLine("Listan sisältö:");
        for (int i = 0; i < END(L); i++)
        {
            Console.Write(L.returnValueAtIndex(i) + " ");
        }
        Console.WriteLine();
    }


    public static void Main(string[] args)
    {
        List testilista = new List();
        INSERT(2, END(testilista), testilista);
        INSERT(7, END(testilista), testilista);
        INSERT(9, END(testilista), testilista);
        INSERT(12, END(testilista), testilista);
        INSERT(9, END(testilista), testilista);
        INSERT(38, END(testilista), testilista);

        Console.WriteLine("testilista");
        PRINT(testilista);

        Console.ReadLine();

    }
}

}

从列表中查找最小值

在 C# 中执行此操作的最简单方法是使用 LinQ:

var minValue = data.Min();

如果需要最高值:

var maxValue = data.Max();

注意:答案并非特定于 C#

给定一个无序列表,查找列表中最小数字的最快方法是查看列表中的每个元素。

var unorderedList = [5,4,3,2,6,7,-23,8,-64,2,0,6];
function findSmallest(anArray){
    var lowest = anArray[0];
    for(var i = 1; i < anArray.length; i++){
        var num = anArray[i];
        if(num < lowest){
            lowest = num;
        }
    }
    return lowest;
}
var smallest = findSmallest(unorderedList);
console.log(smallest); //prints -64

您可以在此处运行代码

点击运行按钮

我认为这不是最好的选择。对我来说,有两种方法。

按此代码对列表进行排序。

int valueMin = L.returnValueAtIndex(0);
for (int i = 0; i < END(L); i++)
{
   //if the value of i is smaller than the value
   if (valueMin < L.returnValueAtIndex(i))
   {
       //i become the min Value
       valueMin = L.returnValueAtIndex(i);
   } 
}
Console.WriteLine(valueMin);
Console.Read();

或者在 C# 中,您可以使用 Array.Sort

Array.Sort(L);
Console.WriteLine(L.returnValueAtIndex(0));
Console.Read();

我希望这对你有帮助!