排序数组算法

编程入门 行业动态 更新时间:2024-10-18 03:26:41
本文介绍了排序数组算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好b $ b 我正在进行排序算法,但是当我测试它时有些不对劲: i60.tinypic/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分钟,对吗? 你只是忘了设置 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: i60.tinypic/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,

更多推荐

排序数组算法

本文发布于:2023-11-30 09:29:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649502.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   算法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!