从列表中查找最小值

时间:2016-02-11 15:45:08

标签: 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();


    }
}

}

3 个答案:

答案 0 :(得分:1)

注意:答案不是特定于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

您可以运行代码here

点击运行按钮

答案 1 :(得分:1)

在C#中最简单的方法是使用LinQ:

var minValue = data.Min();

如果你想要最高价值:

var maxValue = data.Max();

答案 2 :(得分:0)

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

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

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();

我希望这会对你有所帮助!