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

(上記のように) Stopwatch クラスを使用する Vance Morrison のウェブログからのコードがいくつかありますが、複数の実行を行い、統計分析を実行して、平均、中央値のランタイム、および標準的な導出を提供します。

ここで確認してください: リンク

于 2010-10-30T22:00:10.850 に答える