如何在冒泡排序中插入时间跨度?

时间:2019-05-25 20:38:24

标签: c#

我创建了一个程序,可以对随机数执行冒泡排序,并且我试图包括进行每个排序操作所花费的时间(以毫秒为单位)。我尝试了几种方法,但是我的程序甚至不认可它的代码。有人看到我在做什么错吗?

    class BubbleSort
    {
    static void Main(string[] args)
    {
        int[] a = { 50000, 250000, 25000, 750000, 500000, 100, 100000, 
    1000000, 1000 };
        int t;


        Console.WriteLine("Array is: ");
        for (int i = 0; i < a.Length; i++)
        {
            Console.WriteLine(a[i]);
        }
        DateTime start = DateTime.Now;
        DateTime end;
        for (int j = 0; j <= a.Length - 2; j++)
        {
            for (int i = 0; i <= a.Length - 2; i++)
            {
                if (a[i] > a[i + 1])
                {
                    t = a[i + 1];
                    a[i + 1] = a[i];
                    a[i]=t;

                }
            }

        }

        end = DateTime.Now;
        TimeSpan ts = end.Subtract(start);
        Console.WriteLine("The Sorted Array: ");
        foreach (int Array in a)
            Console.Write(Array + " ");
        Console.Read();
        Console.WriteLine("Duration = {0} ms", ts.TotalMilliseconds);
    }

0 个答案:

没有答案