how do I sort randomly generated numbers?

Page 1 of 1

8 Replies - 3524 Views - Last Post: 03 April 2013 - 10:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=317381&amp;s=59ef9a2b1094ce7428a20d294b0acaa7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 am0n

Reputation: 0
• Posts: 40
• Joined: 22-January 13

how do I sort randomly generated numbers?

Posted 01 April 2013 - 12:51 PM

Hello
Here's my code
```    #include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(0));
int random_integer;
int lowest=-10, highest=10;
int range=(highest-lowest)+1;
for(int index=0; index<20; index++){
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << random_integer << ' ';
}

cout<<"\n=============== \n";
system("pause");
}

```

how do I sort the numbers that computer will generate and line them up from lowest to highest?
Thank you.

Is This A Good Question/Topic? 0

Replies To: how do I sort randomly generated numbers?

#2 modi123_1

• Suitor #2

Reputation: 13965
• Posts: 55,737
• Joined: 12-June 08

Re: how do I sort randomly generated numbers?

Posted 01 April 2013 - 12:56 PM

Outside of rolling your own, there is always the sort method in the std library: std::sort.

#3 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,805
• Joined: 19-March 11

Re: how do I sort randomly generated numbers?

Posted 01 April 2013 - 12:59 PM

First of all, you can't sort the items without stowing them in an array, so you'll need to do that. (not just print them to the console)

There are a number of algorithms for sorting. Simplest, conceptually, is the bubble sort, which works by sweeping through the list and comparing adjacent items. If they are out of order, they are swapped. Repeated N times for a list of length N, this will produce a sorted list.
Note that it is not necessary to traverse the entire list each of the N times - there are some details I've left out which should become clear to you if you play with this process a few times. (try pencil and paper)

The first question you might ask yourself first is, in this array {1,4,2,6,7,3,9,8,0,5} what will be the last item in the list after the first iteration? Come up with an answer, then try it.

Bubblesort is the least efficient sort you can come up with, but it's a good place to start.

#4 am0n

Reputation: 0
• Posts: 40
• Joined: 22-January 13

Re: how do I sort randomly generated numbers?

Posted 01 April 2013 - 01:13 PM

Thanks jon, I believe the answer is 1, as I think bubble sort compares two numbers for example it will compare 1 and 4, since 1 is lower than 4 it will stay on same place however, it will replace 4 and 6.
Anyway, I do not have much experience with c++ but how do I sort the randomly generated numbers in my array?

#5 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,805
• Joined: 19-March 11

Re: how do I sort randomly generated numbers?

Posted 01 April 2013 - 02:07 PM

1 is not the answer I was looking for, but I might not have been clear - I meant sorting in ascending order, so for any pair l[i], l[j], "in order" means that l[i]<=l[j].

The bubble sort procedure, at its simplest, does N scans of a list of N items, and on each scan it ensures that for each item l[i] in the list, that it is correctly sorted with respect to l[i+1] by comparing the two and exchanging if necessary. Do this sort by hand, and see how it works, and then try to write the code for it.

So for {1,4,2,6,7,3,9,8,0,5}, the first scan starts out by comparing 1, 4 - they're in order, so it doesn't swap. Then it compares 4, 2 - they're out of order, so it swaps. Then it compares 4, 6, and so forth.

#6 buffalobill

Reputation: 21
• Posts: 188
• Joined: 08-July 08

Re: how do I sort randomly generated numbers?

Posted 02 April 2013 - 11:25 PM

As stated above (#3) you need to load each of the random numbers into a container such as an array or vector. Then you can call a sort method to re-arrange the order as desired.

#7 Midi_

Reputation: 9
• Posts: 111
• Joined: 22-May 12

Re: how do I sort randomly generated numbers?

Posted 02 April 2013 - 11:55 PM

Very carefully haha

put all the random nums into an array(array 1), then find the largest number in array 1, put that number into another array(array2), make the largest number in array 1(the one you put into array 2) the smallest number(like 0), repeat the process till all the numbers in array 1 are 0s and all the numbers in the second array are in order. then set up a loop to print them out. Its kind of a pain, but once you get it its easy.

#8 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,805
• Joined: 19-March 11

Re: how do I sort randomly generated numbers?

Posted 03 April 2013 - 06:19 AM

Midi_, on 03 April 2013 - 01:55 AM, said:

put all the random nums into an array(array 1), then find the largest number in array 1, put that number into another array(array2), make the largest number in array 1(the one you put into array 2) the smallest number(like 0), repeat the process till all the numbers in array 1 are 0s and all the numbers in the second array are in order. then set up a loop to print them out. Its kind of a pain, but once you get it its easy.

Why use two arrays for this? Assuming an ascending sort (which is the usual thing) you'd search the array for the smallest value, and swap it with the value in the zero position. Position zero is now sorted. Search the unsorted portion of the array for the smallest value, and swap it into position 1. [0..1] is now sorted. Repeat until array is sorted.

This is what's called a selection sort. It's somewhat less unreasonable than a bubble sort.

#9 Midi_

Reputation: 9
• Posts: 111
• Joined: 22-May 12

Re: how do I sort randomly generated numbers?

Posted 03 April 2013 - 10:09 AM

jon.kiparsky, on 03 April 2013 - 06:19 AM, said:

Midi_, on 03 April 2013 - 01:55 AM, said:

put all the random nums into an array(array 1), then find the largest number in array 1, put that number into another array(array2), make the largest number in array 1(the one you put into array 2) the smallest number(like 0), repeat the process till all the numbers in array 1 are 0s and all the numbers in the second array are in order. then set up a loop to print them out. Its kind of a pain, but once you get it its easy.

Why use two arrays for this? Assuming an ascending sort (which is the usual thing) you'd search the array for the smallest value, and swap it with the value in the zero position. Position zero is now sorted. Search the unsorted portion of the array for the smallest value, and swap it into position 1. [0..1] is now sorted. Repeat until array is sorted.

This is what's called a selection sort. It's somewhat less unreasonable than a bubble sort.

Yeah I agree, this is just another way of doing it. Sorry for the order, Ive been doing a lot of work with Link lists lately so it goes in and out in reverse compared to the usual way.