我编码了这个
var sw1 = Stopwatch.StartNew();
_selection.ExecuteSelectionSorting(arr, true);
System.Threading.Thread.Sleep(500);
sw1.Stop();
elapsedMs1 = sw1.Elapsed.ToString();
var sw2 = Stopwatch.StartNew();
_insertion.ExecuteInsertionSorting(ar, true);
System.Threading.Thread.Sleep(500);
sw2.Stop();
elapsedMs2 = sw2.Elapsed.ToString();
var sw3 = Stopwatch.StartNew();
_bubble.ExecuteBubbleSorting(number, true);
System.Threading.Thread.Sleep(500);
sw3.Stop();
elapsedMs3 = sw3.Elapsed.ToString();
var sw4 = Stopwatch.StartNew();
MergeSort.MergeSort_Recursive(merge_input, 0, len - 1);
System.Threading.Thread.Sleep(500);
sw4.Stop();
elapsedMs4 = sw4.Elapsed.ToString();
var sw5 = Stopwatch.StartNew();
QuickSort.QuickSort_Recursive(quick_input, 0, len_quick - 1);
System.Threading.Thread.Sleep(500);
sw5.Stop();
elapsedMs5 = sw5.Elapsed.ToString();
我得到它的时间并将其显示在文本框上
time1.Text = elapsedMs1 + " ms";
time2.Text = elapsedMs2 + " ms";
time3.Text = elapsedMs3 + " ms";
time4.Text = elapsedMs4 + " ms";
time5.Text = elapsedMs5 + " ms";
现在我想比较最快的经过时间......
double sort1, sort2, sort3, sort4, sort5;
// try
//{
sort1 = Convert.ToInt32(time1.Text);
sort2 = Convert.ToInt32(time2.Text );
sort3 = Convert.ToInt32(time3.Text);
sort4 = Convert.ToInt32(time4.Text);
sort5 = Convert.ToInt32(time5.Text);
if (sort1 > sort2 && sort1 > sort3 && sort1 > sort4 && sort1 > sort5)
{
MessageBox.Show("The Best Sorting Algorithm is Insertion Sort", "Result",
MessageBoxButtons.OK);
}
else if (sort2 > sort1 && sort2 > sort3 && sort2 > sort4 && sort2 > sort5)
{
MessageBox.Show("The Best Sorting Algorithm is Selection Sort", "Result",
MessageBoxButtons.OK);
}
else if (sort3 > sort1 && sort3 > sort2 && sort3 > sort4 && sort4 > sort5)
{
MessageBox.Show("The Best Sorting Algorithm is Bubble Sort", "Result",
MessageBoxButtons.OK);
}
else if (sort4 > sort1 && sort4 > sort2 && sort4 > sort3 && sort4 > sort5)
{
MessageBox.Show("The Best Sorting Algorithm is Merge Sort", "Result",
MessageBoxButtons.OK);
}
else if (sort5 > sort1 && sort5 > sort2 && sort5 > sort3 && sort5 > sort4)
{
MessageBox.Show("The Best Sorting Algorithm is Quick Sort", "Result");
这是我到目前为止所做的事情,我有点失落...... 谢谢你们的帮助T_T
我总是得到将DateTime转换为Int的错误,你需要解析类似的东西
答案 0 :(得分:0)
将您的double sort1, sort2, sort3, sort4, sort5
更改为long sort1, sort2, sort3, sort4, sort5
,将Convert.ToInt32
更改为long.Parse
您可能应该在全局或类内跟踪经过时间的变量,而不是通过文本框来访问它们。
答案 1 :(得分:0)
大约有一百万种方法可以做到这一点。我将为您提供转换问题的线索,但您的算法需要很好的重构。
不要将sort1..sort5定义为double,而是将它们定义为Timespan:
TimeSpan sort1,sort2,sort3,sort4,sort5;
然后,将您的文本框值转换为:
sort1 = DateTime.Parse(time1.Text).TimeOfDay;
sort2 = DateTime.Parse(time2.Text).TimeOfDay;
依旧...... 之后,您可以根据需要比较排序值:)
修改强>
因为它会引发错误,虽然我已经尝试过上面的代码并且工作得很好,试试这个:
CultureInfo provider = CultureInfo.CurrentCulture;
sort1 = DateTime.ParseExact(time1.Text.Trim(),"hh:mm:ss.fffff",provider).TimeOfDay;
编辑2
这是一个工作(至少在我的计算机中)代码示例:
var sw2 = Stopwatch.StartNew();
System.Threading.Thread.Sleep(500);
string elapsedMs2 = sw2.Elapsed.ToString();
System.Threading.Thread.Sleep(500);
string elapsedMs3 = sw2.Elapsed.ToString();
sw2.Stop();
TimeSpan s1 = DateTime.Parse(elapsedMs2).TimeOfDay;
TimeSpan s2 = DateTime.Parse(elapsedMs3).TimeOfDay;