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
  • Problem: Write a program that generates random numbers between 1 and 10 and fill an array...

    Problem: Write a program that generates random numbers between 1 and 10 and fill an array of size 20 with them. Have your program sort the array then output in order the number of occurrences of each random number generated. Use the STL sort function to sort your array. How to use the STL sort: #include <algorithm> ... int a[20] ... sort(a,a+20); Example Output: This program generates random numbers and tabulates the results Original List Sorted: a[ 0] 1 a[...

  • PYTHON PROGRAMMING: DO NOT USE INPUT FUNCTION, use sys.argv Write a program that generates two random...

    PYTHON PROGRAMMING: DO NOT USE INPUT FUNCTION, use sys.argv Write a program that generates two random integer numbers between 1 and 100. Then, print out the numbers between the two randomly generated ones using a while loop. The output is shown below. Output: a) start:4, end:14 4 5 6 7 8 9 10 11 12 13 14 b) start:15, end:20 15 16 17 18 19 20

  • Suppose there are 100 identical firms in the market and the luggage industry is perfectly competitive....

    Suppose there are 100 identical firms in the market and the luggage industry is perfectly competitive. What does the market supply curve look like? 20 19 18 17 16 15 14 13 12 11 A 10 9 8 7 6 5 4 20 19 18 17 16 15 14 13 12 11 A 10 8 7 6 2 1 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5...

  • Suppose there are 100 identical firms in the market and the luggage industry is perfectly competitive....

    Suppose there are 100 identical firms in the market and the luggage industry is perfectly competitive. What does the market supply curve look like? 20 19 18 17 16 15 14 13 12 11 A 10 20 19 18 17 16 15 14 13 12 11 A 10 8 7 5 2 1 0 1 0 0 1 2 3 4 5 6 7 8 9 10 11 12 4 5 6 7 8 9 10 11 12 0 1...

  • Cork price: 16 10 15 10 17 11 14 13 11 14 11 16 18 16...

    Cork price: 16 10 15 10 17 11 14 13 11 14 11 16 18 16 10 17 14 14 16 7 10 12 19 15 16 14 9 12 21 13 10 16 12 16 13 17 17 13 14 18 11 12 15 16 13 18 16 17 12 12 14 9 11 14 19 13 11 17 11 13 15 14 18 18 18 12 10 11 13 14 11 14 18 13 13 19 17 14...

  • Cork price: 16 10 15 10 17 11 14 13 11 14 11 16 18 16...

    Cork price: 16 10 15 10 17 11 14 13 11 14 11 16 18 16 10 17 14 14 16 7 10 12 19 15 16 14 9 12 21 13 10 16 12 16 13 17 17 13 14 18 11 12 15 16 13 18 16 17 12 12 14 9 11 14 19 13 11 17 11 13 15 14 18 18 18 12 10 11 13 14 11 14 18 13 13 19 17 14...

  • Problem 6. The set (Z19 − {0}, ·19) is a group with the indicated operation; see...

    Problem 6. The set (Z19 − {0}, ·19) is a group with the indicated operation; see the attached table. a.) Show that H = {1, 7, 8, 11, 12, 18} is a subgroup. b.) List all the right cosets of H. c.) Show that if Hy = Hx then xy−1 ∈ H. [Make sure to give a reason for each step.] d.) Show that φ : H → Hx defined by φ(h) = hx is one-to-one and onto. [Use the...

  • Review the 6 karyotypes in Figure 10 and determine the chromosomal disorder. Record the chromosomal disorder...

    Review the 6 karyotypes in Figure 10 and determine the chromosomal disorder. Record the chromosomal disorder in Data Table 3. Describe the genotype of each chromosomal disorder and record in Data Table 3. Describe the phenotype of each chromosomal disorder and record in Data Table 3. Data Table 3: Karyotype to Genotype to Phenotype # Chromosomal Disorder Genotype Phenotype 1 2 3 4 5 6 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7...

  • I need to create a C++ program to simulate a Round Robin Tournament. For example: if...

    I need to create a C++ program to simulate a Round Robin Tournament. For example: if a user enters 4, a 4 team tournament would output: 1 2 3 4 2 1 4 3 3 4 1 2 4 3 2 1 My goal is to create this program using a two dimentional array, however I am unsure how to go about doing initializing everything. How do I write a constructor for this? The following is the class declaration I...

  • You are to write a program that mimics the game memory. You are to do this...

    You are to write a program that mimics the game memory. You are to do this by having two "decks" of cards with values 1-10, randomly inserted into each deck (effectively, you will create two rows of 10 cards each, randomly ordered.) Then, prompt the user to pick a card form each row of cards. If there is a match, the two cards are discarded (an X will appear in the row for each matched pair). The user continues to...

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