没有Array.Sort的sort

时间:2013-03-04 03:57:15

标签: c#

我几乎不知道如何使用if语句和“for循环”。此外,我现在必须这样做

  

将数组中的整数元素从最低(元素0)排序到最高(元素4)。不要使用预先存在的Array.Sort方法;自己编码。

这是一个家庭作业问题,我甚至不知道从哪里开始。有人可以引导我解决这个问题吗?

class Program
{
    static void Main(string[] args)
    {
        int i;
        double power = 0, sum = 0;
        int[] mArray = new int[5];


        Console.WriteLine("Please Enter Number Between 10 and 50 \nMake sure all of your Number entered correctly \notherwise you will need to enter everything again ");


        for (i = 0; i < mArray.Length; i++)
        {
            Console.WriteLine("Please enter your Number.");
            mArray[i] = Convert.ToInt32(Console.ReadLine());

            if (mArray[i] >= 50 || mArray[i] <= 10)
            {
                i--;
                Console.WriteLine("Please enter numbers only between 10 and 50.");

            }

        }

        for (i = 0; i < mArray.Length; i++)
        {
            sum = sum + (mArray[i]);
        }

        double mean = sum / mArray.Length;

        for (i = 0; i < mArray.Length; i++)
        {
            power += Math.Pow((mArray[i] - mean), 2);
        }

        double rMean = power / (mArray.Length - 1);

        Console.WriteLine("Mean {0}", mean);
        Console.WriteLine("Variance {0}", rMean);

        Console.WriteLine("Here is sorted numbers");




        Console.ReadKey();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用许多排序算法Insertion Sort Selection SortBubble SortShell SortMerge SortHeap SortQuick Sort, 还有很多其他人。