排序数组算法 [英] Sorting array algoritms

查看:89
本文介绍了排序数组算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我正在进行排序算法,但是当我测试它时有些不对劲:



http://i60.tinypic.com/98zg5e.jpg [ ^ ]



希望你能看到图片。



它应该在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;
}

解决方案

它看起来是正确的,但写一个测试函数来检查已排序的数组。



编写测试来验证期望是一个专业的范例:-O


将行下面的行移出while循环,因为你重置了开始时间有了它,因此永远不会结束。

 double startTime = GetTickCount(); 





祝你好运!


我假设你想扩展数组直到排序时间超过2分钟,对吗?



你只是忘了设置<$循环中的c $ c> twoMinutesPassed 变量,因此它保持为false。您需要执行以下操作:

  if (totalTime<  120000 
arrayLength * = 1 2 ;
else
twoMinutesPassed = true


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;
}

解决方案

it looks right, but write a test function to check the sorted array.

Writing test to verify "expectations" is a professional paradigma :-O


Move the line below out of the while loop because you reset the start time with this and therefor it will never end.

double startTime = GetTickCount();



Good luck!


I assume you want to extend the array until the sorting time exceeds 2 minutes, correct?

You simply forgot to set the twoMinutesPassed variable in the loop, so it remains on false. You need to do something like

if (totalTime < 120000)
    arrayLength *= 1.2;
else
    twoMinutesPassed = true,


这篇关于排序数组算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆