15,793,452 members
See more:
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:

C++
```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;
}```
Posted
Updated 18-Aug-14 23:18pm
v2

## Solution 1

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

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

## Solution 2

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!

## Solution 3

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
C++
```if (totalTime < 120000)
arrayLength *= 1.2;
else
twoMinutesPassed = true,```