C# 堆排序 ,System.Timers;检查算法时间
我必须检查 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();
}
}
}
我在你的帮助下写了这篇文章,好吗?
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用系统文本;
使用系统诊断;
命名空间排序
{
班级计划
{
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)
{
整数我;
//建立一个堆
for (i = (列表.长度 - 1) / 2;i >=0;i--)
调整(列表, i, 列表.长度 - 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();
}
}
}
I have to check HeapSort algorithm time in C# , my problem is that I Know I must use System.Timers , because I don't know how to measures the algorithm time.
I have to check the algorithm time for table contains 1000 ,10 000 , 100 000 and 1000 000 integers.
Help me good people please.
This is the code:
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(); } } }
I've write this with You help , does is good ?
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(); } } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在寻找时间测量值,请使用
秒表
类。这使您可以使用
Start()
和Stop()
方法轻松测量一些时间。然后,Elapsed
属性将告诉您该操作花费了多长时间。If you're looking for time measurements, use the
Stopwatch
class.This allows you to easily measure some time using the
Start()
andStop()
method. TheElapsed
property will then tell you how long the operation took.您可以使用 Stopwatch 类来测量时间:
You could use the Stopwatch class to measure time:
Vance Morrison 的博客中有一些代码使用 Stopwatch 类(如上所述),但会进行多次运行并执行一些统计分析,以便为您提供平均值、中值运行时间以及标准推导。
在这里查看:
链接
There's some code from Vance Morrison's weblog that uses the Stopwatch class (as described above), but does multiple runs and performs some statistical analysis to give you the mean, median runtime, along with the standard derivation.
Check it out here:
Link