8 Replies - 1315 Views - Last Post: 03 April 2013 - 10:09 AM Rate Topic: -----

#1 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8955
  • View blog
  • Posts: 33,571
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,737
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 am0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,737
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,737
  • Joined: 19-March 11

Re: how do I sort randomly generated numbers?

Posted 03 April 2013 - 06:19 AM

View PostMidi_, 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.
Was This Post Helpful? 0
  • +
  • -

#9 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: how do I sort randomly generated numbers?

Posted 03 April 2013 - 10:09 AM

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

View PostMidi_, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1