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

  • Context:
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Array Numbers
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
needOfHelpCMath
Messages
70
Reaction score
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

count <<  "Generate 500 random numbers:" << endl;
	const int nnn = 500;
	int numberOfArray3[nnn];
		for (i = 0; i < nnn; ++i) {
			numberOfArray3[i] = rand() % 100;
			count << numberOfArray3[i] << " ";
		}
 
on Phys.org
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.
 
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)
{
	count << endl << endl;
	count << "Generate 5000 numbers: " << endl;
	
	for(int i = 0; i  < N; ++i) 
	{
		arr[i] = 0;
		count << arr[i] << " ";	
	}