How Can I Generate and Sort 500 Numbers in an Array?

In summary, the programmer wants to generate 5000 numbers in an array, but in a specific order. They use an algorithm called bubble sort which goes through the list of numbers and swaps the larger number with the smaller number if it comes first.
  • #1
needOfHelpCMath
72
0
I want to generate 500 numbers in an array but in order? For example I can generate 500 random numbers with this code but how will i be able to put it in order?

HTML:
//code example

cout <<  "Generate 500 random numbers:" << endl;
	const int nnn = 500;
	int numberOfArray3[nnn];
		for (i = 0; i < nnn; ++i) {
			numberOfArray3[i] = rand() % 100;
			cout << numberOfArray3[i] << " ";
		}
 
Technology news on Phys.org
  • #2
What do you mean by "in order"? Are you saying that you have generated 500 numbers using a random number generator and then want to order them from smallest to largest? There are a number of algorithms for ordering a set of numbers. Probably the simplest is the "bubble sort":
https://en.wikipedia.org/wiki/Bubble_sort. The "bubble sort" goes through the list repeatedly, comparing each pair of adjacent numbers, if the larger number comes first, the two numbers are swapped, then goes on to the next number. The repetition repeats until it goes through the entire list without any swaps.

For example if the list is 2, 4, 5, 3 then "bubble sort" looks at the first pair, (2, 4). They are in the correct order so it goes on to (4, 5). They are also in the correct order so it goes on to (5, 3). They are not in the correct order so it swaps them to (3, 5) so the list is now 2, 4, 3, 5. Now, repeat. The first pair, (2, 4), is in the correct order so it goes on to the next pair, (4, 3). They are not in the correct order so they are swapped to (3, 4). The list is now 2, 3, 4, 5. The last pair, (4, 5) is in the correct order so it goes back to the beginning and goes through that list, finding no swaps so terminates.

One nice thing about "bubble sort" is that it can be done "in place". If your original list is A then your sorted list will also be in A. You do not have to define a new array to hold the numbers.
 
  • #3
HallsofIvy said:
What do you mean by "in order"? Are you saying that you have generated 500 numbers using a random number generator and then want to order them from smallest to largest? There are a number of algorithms for ordering a set of numbers. Probably the simplest is the "bubble sort":
https://en.wikipedia.org/wiki/Bubble_sort. The "bubble sort" goes through the list repeatedly, comparing each pair of adjacent numbers, if the larger number comes first, the two numbers are swapped, then goes on to the next number. The repetition repeats until it goes through the entire list without any swaps.

For example if the list is 2, 4, 5, 3 then "bubble sort" looks at the first pair, (2, 4). They are in the correct order so it goes on to (4, 5). They are also in the correct order so it goes on to (5, 3). They are not in the correct order so it swaps them to (3, 5) so the list is now 2, 4, 3, 5. Now, repeat. The first pair, (2, 4), is in the correct order so it goes on to the next pair, (4, 3). They are not in the correct order so they are swapped to (3, 4). The list is now 2, 3, 4, 5. The last pair, (4, 5) is in the correct order so it goes back to the beginning and goes through that list, finding no swaps so terminates.

One nice thing about "bubble sort" is that it can be done "in place". If your original list is A then your sorted list will also be in A. You do not have to define a new array to hold the numbers.


Thanks I figure it out my professor wasnt clear on what he wanted... He wanted to print out array that will have the same number.

HTML:
int tArray(int arr[], int N)
{
	cout << endl << endl;
	cout << "Generate 5000 numbers: " << endl;
	
	for(int i = 0; i  < N; ++i) 
	{
		arr[i] = 0;
		cout << arr[i] << " ";	
	}
 

1. How do I generate random numbers in an array?

To generate random numbers in an array, you can use a loop to iterate through the array and use a random number generator function to assign a random number to each element in the array.

2. What is the difference between generating numbers in an array and generating numbers in a list?

The main difference is the data structure used to store the numbers. Arrays are fixed in size and have a specific index for each element, while lists are dynamic and can grow or shrink in size. Arrays are also generally faster for accessing and manipulating data compared to lists.

3. Can I specify a range for the generated numbers in the array?

Yes, you can specify a range for the generated numbers by using a random number generator function that takes in a minimum and maximum value as parameters. This will ensure that the generated numbers fall within the specified range.

4. How can I ensure that the generated numbers are unique in the array?

To ensure that the generated numbers are unique, you can use a conditional statement to check if the number already exists in the array before assigning it. Alternatively, you can use a set data structure to store the generated numbers, as sets do not allow duplicate values.

5. Are there any built-in functions for generating numbers in an array?

Yes, many programming languages have built-in functions for generating numbers in an array, such as the "rand()" function in C++ or the "random()" function in Python. These functions are often efficient and reliable, so it is recommended to use them instead of creating your own random number generator function.

Similar threads

  • Programming and Computer Science
Replies
4
Views
783
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
22
Views
3K
Back
Top