Click here to Skip to main content
14,938,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm working on a sorting algoritm but something isn't right when I test it:[^]

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;


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;
Updated 18-Aug-14 22:18pm

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;
    twoMinutesPassed = true,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900