Bubble Sorting Random Integer Generator

Ok I know how to do bubble sorting but I forgot how to do the random integers part. I'm supposed to prompt the user for a number, creates an array for that number of random integers, and then uses the bubble sort to order the array. What I did was prompt the user for a number for the array and have them feel it in.

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
#include<iostream>

using namespace std;

int main()
{
	int a[50], n, i, j, temp;
	cout << "Enter the size of array: ";
	cin >> n;
	cout << "Enter the array elements: ";

	for (i = 0; i<n; ++i)
		cin >> a[i];

	for (i = 1; i<n; ++i)
	{
		for (j = 0; j<(n - i); ++j)
			if (a[j]>a[j + 1])
			{
				temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
	}

	cout << "Array after bubble sort:";
	for (i = 0; i<n; ++i)
		cout << " " << a[i];
	cout << endl;
	system("pause");
	return 0;
}
Hi Starfire

I have created some time ago basic function for this problem, its nothing fancy but it worked for my exercise :)
1
2
3
4
5
6
7
8
9
void fillArrayWithValues(int* p_fill, int sizeOfArray)
{
	for (int i = 0; i < sizeOfArray; i++)
	{
		int random_integer = rand();
		p_fill[i] = random_integer;

	}
}
Thanks but now I am even more confused. I haven't coded in over 2 years and my discrete structures teacher wanted us to do this for a project and I am utterly lost and frustrated.
its a function, a subroutine.. put it above main and replace your cin loop with

fillArrayWithValues(a,50);

you may need some include to grant rand().
Last edited on
So basically like this?


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
#include<iostream>

using namespace std;

void fillArrayWithValues(int* p_fill, int sizeOfArray)
{
	for (int i = 0; i < sizeOfArray; i++)
	{
		int random_integer = rand();
		p_fill[i] = random_integer;

	}
}

int main()
{
	int a[50], n, i, j, temp;
	cout << "Enter the size of array: ";
	cin >> n;

	for (i = 0; i<n; ++i)
		fillArrayWithValues(a, 50);

	for (i = 1; i<n; ++i)
	{
		for (j = 0; j<(n - i); ++j)
			if (a[j]>a[j + 1])
			{
				temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
	}

	cout << "Array after bubble sort:";
	for (i = 0; i<n; ++i)
		cout << " " << a[i];
	cout << endl;
	system("pause");
	return 0;
}
Lines 27-31, the sorting is not being done on the i'index at all. Also the way fillArrayWithValues is defined your array size is hard-coded to be 50, so there's no need to have the “Enter size of array” bit. If you want a user-defined array size then instead of int a[50] you'd have to use a dynamic array or a standard library container like std::vector<int>

re random number generation modern C++ recommends using the <random> library functions instead of the c-style rand() (btw you also need to #include <cstdlib> for rand() in any case and use a seed or else you'll be getting the same 'random' numbers every time).

you could also use std::swap instead of declaring an additional variable int temp to sort the container if you are using a c-style array and std::sort to sort the std::vector<int> outright.
here's the corrected code for the sort from the above program:
1
2
3
4
5
6
7
8
9
10
for (i = 0; i<n; ++i)
	{
		for (j = i+1; j < n ; ++j)
			if (a[j] < a[i])
			{
				temp = a[i];
				a[i] = a[j ];
				a[j] = temp;
			}
	}
Last edited on
I want the use to come up with a size as long as it doesn't go over how big the array can get. I did so more coding and so far it seems to be working how I want it to go since the limit is 10,000.

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
#include<iostream>

using namespace std;

void fillArrayWithValues(int* p_fill, int sizeOfArray)
{
	for (int i = 0; i < sizeOfArray; i++)
	{
		int random_integer = rand();
		p_fill[i] = random_integer;

	}
}

int main()
{
	int a[10000], n, i, j, temp;
	cout << "Enter the size of array: ";
	cin >> n;

	for (i = 0; i<n; ++i)
		fillArrayWithValues(a, n);

	for (i = 1; i<n; ++i)
	{
		for (j = 0; j<(n - i); ++j)
			if (a[j]>a[j + 1])
			{
				temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
	}

	cout << "Array after bubble sort:";
	for (i = 0; i<n; ++i)
		cout << " " << a[i];
	cout << endl;
	system("pause");
	return 0;
}
Starfire,
if I understand you, you want user to decide how large is the array? right?
or do you want to have hard coded size?

bear in mind that array in this form (to be exact size of array) needs to be know at compile time. Not during runtime, in order to have array size changed at runtime, you have to use dynamic memory allocation with command new.

well, what he did will work, up to a max size of (10k).
you can overallocate an array and only use part of it.

new is better though. Overallocated have a smaller max size, due to stack vs heap. new can allocate all your ram, array can only tap a few MB.
Last edited on
This is my instructions: Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then uses the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. Write a computer program that prompts the user for two numbers, n for the number of items in the array to sort, and num_i for a number of arrays of this size to sort. Then do the following:
Initiate a variable running_time to 0

Create a for loop that iterates num_i times.

In the body of the loop,

Create an array of n random integers

Get the time and set this to start-time. You will have to figure out what the appropriate command is in the programming language you are using to find them time

Use bubble sort to sort the array

Get the time and set this to end-time

Subtract end-time from start-time and add the result to running_time

Once the program has run, note

The number of items sorted
The number of iterations
The average running time for each size array
Repeat the process nine times, using 50, 250 and 500 as the size of the array, and 100, 1000 and 10,000 as the number of arrays of each size to sort.

I feel as though the bubble sort is fine now. However I am working on making my selection sort work.
Jonnin,

My tutor stressed quite strongly not to over allocate array, as bad coding practise. It uses a lot of memory, even when you might need fraction of what you will reserve. On desktop PC this might not manifest as an issue as there is plenty of resources but on mobile phone or some other hardware with limited resources (Arduino, smartwatch?) this could prove problematic.

Anyway as I said I can only base my opinion for now on advise I got from my tutor or what I read in books or on forums like here. :)


Starfire - so yes, it appears that using hard coded 10K is not what your tutor ask.
You have to use new command to reserve dynamically correct size of memory during runtime not compile time.

Thanks for your help guys but I'm still lost on everything so you know what? I'm quitting. I'm done with this project. I'm just going to turn in what I've done and leave it alone.
Topic archived. No new replies allowed.