所以,我有所有的搜索算法,我向每个算法发送随机的 20000 个数字,试图弄清楚每个算法需要多长时间。
public void functionsForSorts(int[] array)
{
Stopwatch sw = new Stopwatch();
long elapsedTime = sw.ElapsedTicks;
if (array.Length == 20000)
{
sw.Start();
BubbleSort.Bubble(array);
sw.Stop();
elapsedTime = sw.ElapsedMilliseconds;
label1.Text += "\t" + elapsedTime.ToString() + " miliseconds ";
Application.DoEvents();
sw.Restart();
SelectionSort.Selection(array);
sw.Stop();
elapsedTime = sw.ElapsedMilliseconds;
label2.Text += "\t" + elapsedTime.ToString() + " miliseconds ";
Application.DoEvents();
sw.Restart();
InsertionSort.Insertion(array);
sw.Stop();
elapsedTime = sw.ElapsedMilliseconds;
label3.Text += "\t" + elapsedTime.ToString() + " miliseconds ";
Application.DoEvents();
sw.Restart();
MergeSort.mergeSort(array, 0, array.Length - 1);
sw.Stop();
elapsedTime = sw.ElapsedMilliseconds;
label4.Text += "\t" + elapsedTime.ToString() + " miliseconds ";
Application.DoEvents();
sw.Restart();
ShellSort.Shell(array);
sw.Stop();
elapsedTime = sw.ElapsedMilliseconds;
label5.Text += "\t" + elapsedTime.ToString() + " miliseconds ";
Application.DoEvents();
sw.Restart();
QuickSort.Quicksort(array, 0, array.Length - 1);
sw.Stop();
elapsedTime = sw.ElapsedMilliseconds;
label6.Text += "\t" + elapsedTime.ToString() + " miliseconds ";
Application.DoEvents();
}
问题是秒表不会给出正确的结果,它适用于冒泡排序、选择排序和合并排序,但我不知道为什么,它总是为插入排序写入 0,即使它在调试时具有正确的值。并且它也没有为 shell 排序和快速排序提供正确的值。
另一个尴尬的部分是,当我注释掉气泡和选择排序时,插入会给出正确的结果,这对所有算法都是如此,如果我按顺序将它们设为第一个,我会得到正确的结果,我把这个展示给我的朋友,他们也没有任何线索,这真的没有任何意义......