gpt4 book ai didi

C# heapSort ,System.Timers;检查算法时间

转载 作者:行者123 更新时间:2023-11-30 13:41:55 24 4
gpt4 key购买 nike

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

请好心人帮帮我

这是代码:


using System;<p></p>

<pre><code>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();
}
}
}
</code></pre>

<p></p>

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

<p></p>

<p>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;</p>

<p>namespace Sort
{
class Program
{</p>

<pre><code> 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)
</code></pre>

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

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

<p>}</p>

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




}


}
}
</code></pre>

<p></p>

最佳答案

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

这使您可以使用 Start()Stop() 方法轻松测量一些时间。 Elapsed 属性将告诉您操作花费了多长时间。

关于C# heapSort ,System.Timers;检查算法时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4060588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com