Question

For C++ Write a program that randomly generates 100 integers and sorts them using radix sort....

For C++

Write a program that randomly generates 100 integers and sorts them using radix sort.

Note: Your output would not be the same as this sample output due to the randomness.

Sample output:

0 0 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 3
3 3 3 4 4 4 4 4 4 4
4 5 5 5 6 6 6 6 6 6
7 7 7 7 7 7 8 8 8 8
8 9 9 9 9 9 9 10 10 10
10 11 11 11 11 11 11 12 12 13
13 13 13 13 13 14 14 14 15 15
15 15 16 16 16 16 16 17 17 17
18 18 18 18 18 18 18 19 19 19

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* The below program generates 100 random numbers from 0 to 19 and then sort them using radix sort. Note that you can generate random numbers of any range by changing the rand%20 in main*/

#include<iostream>
#include<time.h>
using namespace std;


int maximum(int array[], int n)
{
   int mx = array[0];
   for (int i = 1; i < n; i++)
       if (array[i] > mx)
           mx = array[i];
   return mx;
}

void countSort(int array[], int n, int exp)
{
   int output[n];
   int i, count[10] = {0};

   for (i = 0; i < n; i++)
       count[ (array[i]/exp)%10 ]++;

   for (i = 1; i < 10; i++)
       count[i] += count[i - 1];

   for (i = n - 1; i >= 0; i--)
   {
       output[count[ (array[i]/exp)%10 ] - 1] = array[i];
       count[ (array[i]/exp)%10 ]--;
   }

   for (i = 0; i < n; i++)
       array[i] = output[i];
}

void radixsort(int array[], int n)
{
   int m = maximum(array, n);

   for (int exp = 1; m/exp > 0; exp *= 10)
       countSort(array, n, exp);
}

void print(int array[], int n)
{
   for (int i = 0; i < n; i++) {
   if(i%10==0&&i!=0)
       cout<<endl;
       cout << array[i] << " ";
      
   }
}
int main()
{ srand(time(0));
   int array[100];
   for(int i=0;i<100;i++)
   array[i]=rand()%20;
   int n = sizeof(array)/sizeof(array[0]);
   radixsort(array, n);
   print(array, n);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
For C++ Write a program that randomly generates 100 integers and sorts them using radix sort....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT