c# - 从列表中查找最小值

标签 c# algorithm list min

我如何在这里以最有效的方式构建算法以从列表中找到最小值?我知道这个列表还没有以最好的方式完成,但是,有什么办法吗? 我尝试了几种方法,但似乎没有让它有效地工作..

谢谢。

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# - 从列表中查找最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35343776/

相关文章:

c# - 验证数字到小数点后两位的正则表达式

c# - 显示 PDF - 应用程序页面 SharePoint

c++ - 在没有蛮力的情况下反转 SHA-1 或 MD5 给定的(小)输入长度

python - 这里可能有更好的算法吗?

algorithm - 在给定旧 MST 和新顶点 + 边的情况下查找最小生成树

python - 比较相同索引python中列表中的项目

python - 给定一个单词列表,用它们创建短语的子集

python - 从元组中提取边集

c# - 如何在 ListView 中创建可点击的链接?

c# - 在 .NET 中处理大型 csv 的最有效方法