3

我必须在 C# 中检查 HeapSort 算法时间,我的问题是我知道我必须使用 System.Timers ,因为我不知道如何测量算法时间。我必须检查包含 1000 ,10 000 , 100 000 和 1000 000 个整数的表的算法时间。

请帮助我的好人。

这是代码:


    using System;

namespace Sort
{
    class Program
    {
        public static void Adjust(int[] list, int i, int m)
        {
            int Temp = list[i];
            int j = i * 2 + 1;

            while (j <= m)
            {
                if (j < m)
                    if (list[j] < list[j + 1])
                        j = j + 1;
                if (Temp < list[j])
                {
                    list[i] = list[j];
                    i = j;
                    j = 2 * i + 1;
                }
                else
                {
                    j = m + 1;
                }
            }

            list[i] = Temp;
        }

        public static void HeapSort(int[] list)
        {
            int i;
            //Boulding a heap
            for (i = (list.Length - 1) / 2; i >= 0; i--)
                Adjust(list, i, list.Length - 1);

            for (i = list.Length - 1; i >= 1; i--)
            {
                int Temp = list[0];
                list[0] = list[i];
                list[i] = Temp;
                Adjust(list, 0, i - 1);
            }
        }

        static void Main(string[] args)
        {
            Console.Title = "HeapSort";
            int i;
            int[] a = { 12, 3, -12, 27, 34, 23, 1, 81, 45,
                    17, 9, 23, 11, 4, 121 };
            Console.WriteLine("Data before sort ");
            for (i = 0; i < a.Length; i++)
                Console.Write(" {0} ", a[i]);
            Console.WriteLine();
            HeapSort(a);
            Console.WriteLine("Data after sort");
            for (i = 0; i < a.Length; i++)
                Console.Write(" {0} ", a[i]);
            Console.ReadLine();
        }
    }
}

我在你的帮助下写了这个,好吗?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics;

namespace Sort { class Program {

    public static void Adjust(int[] list, int i, int m)
    {
        int Temp = list[i];
        int j = i * 2 + 1;

        while (j <= m)
        {

            if (j < m)
                if (list[j] < list[j + 1])
                    j = j + 1;


            if (Temp < list[j])
            {
                list[i] = list[j];
                i = j;
                j = 2 * i + 1;
            }
            else
            {
                j = m + 1;
            }
        }

        list[i] = Temp;
    }





    public static void HeapSort (int[] list)

{ int i; //Boulding a heap for (i = (list.Length - 1) / 2;i >=0;i--) Adjust (list, i, list.Length - 1);

for ( i = list.Length - 1; i >= 1; i--)
{
    int Temp = list [0];
    list [0] = list [i];
    list [i] = Temp;
    Adjust (list, 0, i - 1);
}

}

    static void Main(string[] args)
    {
        Console.Title = "HeapSort";
        int i;
        Random myRandom = new Random();//Creating instance of class Random
        Stopwatch myTime = new Stopwatch(); //variable for time measurement




        int[] a = new int[1000]; //table contents 1000 variables


        for (i = 0; i < a.Length; i++)
            a[i] = myRandom.Next(100);

        Console.WriteLine("Data before sort ");
        for (i = 0; i < a.Length; i++)
            Console.Write(" {0} ", a[i]);
        Console.WriteLine();
        myTime.Start();
        HeapSort(a);
        myTime.Stop();

        string TimeEl = myTime.Elapsed.ToString();

        Console.WriteLine("Data after sort");
        for (i = 0; i < a.Length; i++)
            Console.Write(" {0} ", a[i]);
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("time elapsed: {0} ", TimeEl);
        Console.ReadLine();




    }


    }
}

4

3 回答 3

6

如果您正在寻找时间测量,请使用Stopwatch该类。

Start()这使您可以使用andStop()方法轻松测量一些时间。然后Elapsed酒店会告诉您手术需要多长时间。

于 2010-10-30T20:40:51.320 回答
4

您可以使用Stopwatch类来测量时间:

var watch = Stopwatch.StartNew();
SomeFunctionThatCallsYourAlgorithm();
watch.Stop();
Console.WriteLine("algorithm execution time: {0}ms", watch.ElapsedMilliseconds);
于 2010-10-30T20:41:31.813 回答
0

Vance Morrison 的博客中有一些代码使用 Stopwatch 类(如上所述),但会执行多次运行并执行一些统计分析,以便为您提供平均值、中值运行时间以及标准推导。

在这里查看: 链接

于 2010-10-30T22:00:10.850 回答