我想计算使用C#编写的冒泡排序算法所需的时间,但结果一直为0。以下是我的代码:
在主函数中,时间总是为0。我在这段代码中犯了什么错误。
public void bubbleSort(int[] arr, ref double time)
{
var sp = new Stopwatch();
sp.Start();
int temp = 0;
for (int i = 0; i < arr.Length; i++)
{
for (int sort = 0; sort < arr.Length - 1; sort++)
{
if (arr[sort] > arr[sort + 1])
{
temp = arr[sort + 1];
arr[sort + 1] = arr[sort];
arr[sort] = temp;
}
}
}
sp.Stop();
time = sp.Elapsed.Milliseconds*1000;
}
在主函数中,时间总是为0。我在这段代码中犯了什么错误。