# Sorting Random Numbers

• 12-06-2002
kid kash
Sorting Random Numbers
Hi, 1st time poster.
I'm currently working on a program that generates random numbers 1-49. I want to sort them from lowest to highest, how would i come about doing that?
here's what i got so far
Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> //for the seedrnd() function #define RANGE 49 //number of numbers #define BALLS 7 //number of balls to draw #define DELAY 1000000 //delay interval between picks int rnd(int range); char loop; void seedrnd(void); void main() {  do {     int numbers[RANGE]; //array that holds the balls     int i,b;     unsigned long d; //delay variable     seedrnd(); //seed the randomizer /* initialize the array */     for(i=0;i<RANGE;i++) //initialize the array         numbers[i]=0;       printf("Press Enter to pick this week's numbers:");     getchar(); /* draw the numbers */     for(i=0;i<BALLS;i++)     {         for(d=0;d<=DELAY;d++); //pause here /* picks a random number and check to see whether it's already been picked */         do         {              b=rnd(RANGE); //draw number         }         while(numbers[b]); //already drawn?         numbers[b]=1; //mark it as drawn          printf(" %i ",b+1); //add one for zero     }     printf("\n Press 1 to return");     scanf("%d", &loop); } while (loop==1); } /* Generate a random value */ int rnd(int range) {     int r;     r=rand()%range; //spit up random number     return(r); } /* Seed the randomizer */ void seedrnd(void) {     srand((unsigned)time(NULL)); }```
• 12-06-2002
kermi3
Ok...First of all welcome, anything I can do to help you on these boards please let me know. Second of all: Thank you very much for reading the Stickies and announcements heh. I really appreciate that.

Now, just an FYI, I edited your code slighty, I just made it more readable. What it looks like you need now is sort it. There are several diffrent sorting algorithms you can use. I would use google to take a look at some of them, or your text book. The easiet one to use is called bubble sort or insertion sort.

I hope this helps you start. Good luck, feel free to ask if you have anymore problems.

Kermi3
• 12-07-2002
PJYelton
Since you are only sorting a small amount of numbers, I'd suggest looking for information on bubble sort. Its the easiest of all the sorts out there to implement (although also probably the slowest, but that won't matter here). It basically goes like this:
Code:

```for (int x=0; x<arraysize; x++)   for (int y=x+1; y<arraysize; y++)       if (array[x]>array[y]) then swap the two```
• 12-07-2002
stautze
here's an insertion sort

Code:

```template<class T> void insertionSort(std::list<T>& listRef) {   list<T>::iterator iter1, iter2, iter3;   iter1 = listRef.end();     for(iter2 = listRef.begin(); iter2 != iter1; iter2 = listRef.erase (iter2)) {     iter3 = listRef.begin();     while(iter3 != listRef.end() && *iter2 > *iter3)       iter3++;     listRef.insert(iter3, *iter2);   }  }```
• 12-07-2002
Kupo