Bubble Sort & Elapsed Time

The premise is to run BubbleSort with a small array size of 10, a medium size of 1000, and a large size of 10,000, and to see how long it took, in milliseconds (elapsed time).

I am having trouble getting my code to compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include <stdlib.h>
#include <ctime>

using namespace std;
const int smallSize = 10;
const int mediumSize = 1000;
const int largeSize = 10000;

void bubbleSort(int myArray[], int smallSize);
void bubbleSort(int myArray[], int mediumSize);
void bubbleSort(int myArray[], int largeSize);

int main()
{
	int size;
	clock_t start, end;
	long timeElapsed;

	srand(NULL);					

	// Bubble Sort Small Size
	int myArray[smallSize];
	for (int randomIntegers = 0; randomIntegers < smallSize; randomIntegers++ )	
    myArray[randomIntegers] = rand();
 
	start = clock();				
	bubbleSort(myArray, smallSize);	
	end = clock();					
	timeElapsed = end - start;		
 
	cout << "Bubble Sort Small Size" << endl;
	cout << "Start Time (milliseconds):  " << start << endl;
	cout << "End Time (milliseconds):  " << end << endl;
	cout << "Total Time (milliseconds):  " << timeElapsed << endl << endl;

	// Bubble Sort Medium Size
	int myArray[mediumSize];
	for (int randomIntegers = 0; randomIntegers < mediumSize; randomIntegers++ )	
    myArray[randomIntegers] = rand();
 
	start = clock();					
	bubbleSort(myArray, mediumSize); 
	end = clock();						
	timeElapsed = end - start;		
 
	cout << "Bubble Sort Medium Size" << endl;
	cout << "Start Time (milliseconds):  " << start << endl;
	cout << "End Time (milliseconds):  " << end << endl;
	cout << "Total Time (milliseconds):  " << timeElapsed << endl << endl;

	// Bubble Sort Large Size
	int myArray[largeSize];
	for (int randomIntegers = 0; randomIntegers < largeSize; randomIntegers++ )	
    myArray[randomIntegers] = rand();
 
	start = clock();					
	bubbleSort(myArray, smallSize);		
	end = clock();						
	timeElapsed = end - start;			
 
	cout << "Bubble Sort Large Size" << endl;
	cout << "Start Time (milliseconds):  " << start << endl;
	cout << "End Time (milliseconds):  " << end << endl;
	cout << "Total Time (milliseconds):  " << timeElapsed << endl << endl;

	cin.get();
	cin.ignore();
	return 0;
}

void bubbleSort(int myArray[], int size) 
	{
		bool swaps = true;
		while (swaps)			
			{
				swaps = false;	// Initialize
				for (int i = 0; i < size - 1; i++) 
					{
						if (myArray[i] > myArray[i+1]) 
							{
								int tempValue = myArray[i];
								myArray[i] = myArray[i + 1];
								myArray[i + 1] = tempValue;
								swaps = true;
							}
					}
			}
	}
Last edited on
Any help is very much appreciated.
Lines 23,38,53: You're trying to define myArray 3 different times in the same scope (main). I suggest you break your program into three functions.

edit: Lines 10-12: You don't need to declare bubbleSort 3 times. All three lines are identical to the compiler. The name of the argument makes no difference in a declaration.

When you say something "doesn't compile", please post the exact error messages from your compiler.
Last edited on
Is this a proper fix?

The only thing is, when I compile, the delta time value is zero. (time elapsed)
That should not be.

What is wrong?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include <stdlib.h>
#include <ctime>

using namespace std;
const int smallSize = 10;
const int mediumSize = 1000;
const int largeSize = 10000;

void bubbleSort(int myArray[], int size);

int main()
{
	
	clock_t start, end;
	long timeElapsed;

	srand(NULL);					// Seed

	// Bubble Sort Small Size
	start = clock();
	void bubbleSmall();
	end = clock();						// End Clock
	timeElapsed = end - start;			// Elapsed Time
	
	cout << "Bubble Sort Small Size" << endl;
	cout << "Start Time (milliseconds):  " << start << endl;
	cout << "End Time (milliseconds):  " << end << endl;
	cout << "Total Time (milliseconds):  " << timeElapsed << endl << endl;

	// Bubble Sort Medium Size
	start = clock();
	void bubbleMedium();
	end = clock();						// End Clock
	timeElapsed = end - start;			// Elapsed Time

	cout << "Bubble Sort Medium Size" << endl;
	cout << "Start Time (milliseconds):  " << start << endl;
	cout << "End Time (milliseconds):  " << end << endl;
	cout << "Total Time (milliseconds):  " << timeElapsed << endl << endl;


	cin.get();
	cin.ignore();
	return 0;
}

void bubbleSort(int myArray[], int size) 
	{
		bool swaps = true;
		while (swaps)			// Start Bubble Sort
			{
				swaps = false;	// Initialize
				for (int i = 0; i < size - 1; i++) 
					{
						if (myArray[i] > myArray[i+1]) 
							{
								int tempValue = myArray[i];
								myArray[i] = myArray[i + 1];
								myArray[i + 1] = tempValue;
								swaps = true;
							}
					}
			}
	}

void bubbleSmall(int myArray[], int size)
	{
		for (int randomIntegers = 0; randomIntegers < smallSize; randomIntegers++ )	
		 myArray[randomIntegers] = rand();
	}

void bubbleMedium(int myArray[], int size)
	{
		
		for (int randomIntegers = 0; randomIntegers < mediumSize; randomIntegers++ )	
		myArray[randomIntegers] = rand();
 
	}
Last edited on
Lines 22 and 33 are declarations, not function calls. They don't do anythying other than tell the compiler how the respective functions should be called.

Your implementations at lines 67 and 73 don't match the declarations nor do they call bubbleSort.

myArray has disappeared. You haven't defined it anywhere.

@AbstractionAnon
Can you show me how to make these corrections please for my future knowledge in programming? I'm not quite sure how to fix those mistakes. Thank you.
I could show you how do do it, but you will learn more if you try and do it yourself. I've told you what's wrong. Fix those errors and post your new code. We will go from there.
@AbstractionAnon I honestly have no clue where to begin. That is why I asked for your assistance. Thanks again for responding.
Last edited on
You could probably begin with whatever resource(s) you're using to learn. I'm sure one of them tells you how to invoke functions. If not find yourself some new resources.

http://www.cplusplus.com/doc/tutorial/functions/
@cire I'm confused, so on lines 22 and 33 do I need to make them function calls?
If you wish to call the function yes. Right now you are just prototyping and not calling them. Generally you prototype beneath your includes. To call a function you don't give a return type and you want to actually pass the parameters. Anyways your prototypes don't match the functions you declared later on.
Last edited on
Is this the right way to fix line's 22 and 33?

Line 22: bubbleSmall(myArray[], smallSize);
Line 33: bubbleMedium(myArray[], mediumSize);

It's hard to visualize my code since I made so many errors that I'm not quite sure what is wrong where.
Last edited on
You aren't declaring an array, you are passing it. So you would simply put myArray. Anyways, I don't think you understand functions and should read the link cire mentioned. The parameters are local to the function so when you pass a to function b it is now variable c not a. You should just use one bubble sort function and pass the size and not have different ones for different sizes that have the same inside. That would make functions pointless.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void bubbleSort(int array[], int size); //function prototype/definition
void bubbleSort(int [], int); //alternative function prototype
void bubbleSort(int *, int); //alternative function prototype

bubbleSort(someArray, someSize); //function call

void bubbleSort(int array[], int size) //names don't need to match prototype
//but these are the variable names being used in the function
{
    //bubble sort here
}

//now to generate arrays:
void fillArray(int array[], int size); //prototype, I prefer to give names so I know what the parms are

fillArray(someArray, sizeOfArray); //function call

void fillArray(int array[], int size) //function declaration, you can use constant for size but must match prototype
{
    for(int i = 0; i < size; ++i)
        array[i] = rand();
}
Last edited on
This is what I have now. It still isn't compiling. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <stdlib.h>
#include <ctime>

using namespace std;
const int smallSize = 10;
const int mediumSize = 1000;
const int largeSize = 10000;

void bubbleSort(int myArray[], int size);

int main()
{
	
	clock_t start, end;
	long timeElapsed;

	srand(NULL);					// Seed

	// Bubble Sort Small Size
	start = clock();
	bubbleSort(bubbleSmall,smallSize);
	end = clock();						// End Clock
	timeElapsed = end - start;			// Elapsed Time
	
	cout << "Bubble Sort Small Size" << endl;
	cout << "Start Time (milliseconds):  " << start << endl;
	cout << "End Time (milliseconds):  " << end << endl;
	cout << "Total Time (milliseconds):  " << timeElapsed << endl << endl;

	// Bubble Sort Medium Size
	start = clock();
	bubbleSort(bubbleMedium,mediumSize);
	end = clock();						// End Clock
	timeElapsed = end - start;			// Elapsed Time

	cout << "Bubble Sort Medium Size" << endl;
	cout << "Start Time (milliseconds):  " << start << endl;
	cout << "End Time (milliseconds):  " << end << endl;
	cout << "Total Time (milliseconds):  " << timeElapsed << endl << endl;


	cin.get();
	cin.ignore();
	return 0;
}

void bubbleSort(int myArray[], int size) 
	{
		bool swaps = true;
		while (swaps)			// Start Bubble Sort
			{
				swaps = false;	// Initialize
				for (int i = 0; i < size - 1; i++) 
					{
						if (myArray[i] > myArray[i+1]) 
							{
								int tempValue = myArray[i];
								myArray[i] = myArray[i + 1];
								myArray[i + 1] = tempValue;
								swaps = true;
							}
					}
			}
	}

void fillArray(int myArray[],int size);
	fillArray(bubbleSmall,smallSize);

void fillArray(int myArray[],int size);
	fillArray(bubbleMedium,mediumSize);

void fillArray(int myArray[],int size);
	fillArray(bubbleLarge,largeSize);

void fillArray(int myArray[], int size)
	{
		for (int i = 0; i < size; i++ )	
		myArray[i] = rand();
	}
This is what I have now. It still isn't compiling. What am I doing wrong?

You begin discovering what is wrong by paying attention to the output generated by your compiler when it fails to compile something. What does your compiler have to say?
I am totally lost. I have been trying to compile my code myself for well over 8 hours now.

@cire I understand. However, the error messages generated are not descriptive enough.
Last edited on
I understand. However, the error messages generated are not descriptive enough.

Then maybe you could share them so you can get some help interpreting them.
I figured it out. Thank you. I will incorporate the gist of this test code into a switch statement that contains other types or sorting.
Last edited on
Topic archived. No new replies allowed.