本文介绍了排序数组算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好b $ b
我正在进行排序算法,但是当我测试它时有些不对劲:
[]
希望你能看到图片。
它应该在2分钟后停止,但事实并非如此。我不知道我是否错了时间。
这是一个插入算法。我也打算使用泡泡和选择,但我现在专注于插入。
所以它排序10真的是对的吗2.6秒后的000 000元素?或者你能否发现代码中的任何其他错误?
我的代码:
void simpleSort( double * arr, int 长度)
{
int j;
double 个数字;
for ( int i = 0 ; i< length; i ++)
{
j = i;
while (j> 0 && arr [j - 1 ]> arr [j])
{
numbers = arr [j];
arr [j] = arr [j- 1 ];
arr [j- 1 ] =数字;
j--;
}
}
}
void main()
{
int arrayLength = 1000 跨度>;
bool twoMinutesPassed = false ;
while (twoMinutesPassed == false )
{
double * arrTimeCheck = new double [arrayLength];
for ( int i = 0 ; i< arrayLength; i ++)
{
double randNumber = rand()/ RAND_MAX;
arrTimeCheck [i] = randNumber;
}
// 检索自系统以来经过的毫秒数已启动,最多49.7天。
double startTime = GetTickCount();
simpleSort(arrTimeCheck,arrayLength);
double endTime = GetTickCount();
double totalTime = endTime - startTime;
double totalTimeSec = totalTime / 1000 ;
cout<< totalTimeSec<< sek .....<< arrayLength<< ENDL;
if (totalTime< 120000)
arrayLength * = 1 。 2 ;
delete [] arrTimeCheck;
}
解决方案
Hi
I'm working on a sorting algoritm but something isn't right when I test it:
http://i60.tinypic.com/98zg5e.jpg[^]
Hope you can see the picture.
It should stop at 2 min mark but it doesn't. I don't know if I got the time wrong or something.
This is a insert-algoritm. I'm also going to work with bubble and selection, but I'm focused on the insert right now.
So can it really be right that it has sorted 10 000 000 elements after 2.6 sec? Or can you spot any other errors in the code?
My code:
void simpleSort( double* arr, int length ) { int j; double numbers; for( int i=0; i<length; i++) { j = i; while(j > 0 && arr[j - 1] > arr[j]) { numbers = arr[j]; arr[j] = arr[j-1]; arr[j-1] = numbers; j--; } } } void main() { int arrayLength = 1000; bool twoMinutesPassed = false; while (twoMinutesPassed == false) { double* arrTimeCheck = new double[arrayLength]; for(int i=0; i < arrayLength; i++) { double randNumber = rand()/RAND_MAX; arrTimeCheck[i] = randNumber; } //Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days. double startTime = GetTickCount(); simpleSort(arrTimeCheck, arrayLength); double endTime = GetTickCount(); double totalTime = endTime - startTime; double totalTimeSec = totalTime / 1000; cout << totalTimeSec << " sek ....." << arrayLength << endl; if(totalTime <120000) arrayLength *= 1.2; delete[] arrTimeCheck; }
解决方案
这篇关于排序数组算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!