Question

IN C++: Create sets of 1 Million integers with the following characteristics; Sets where no numbers...

IN C++:

Create sets of 1 Million integers with the following characteristics;
Sets where no numbers repeat
Sets where the range of numbers is 1% of the array size
Sets where no numbers repeat and each integer has 20 digits

Create Radixsort

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

// C++ implementation of Radix Sort
#include<iostream>
using namespace std;

// A utility function to get maximum value in arr[]
int getMax(int arr[], int n)
{
   int mx = arr[0];
   for (int i = 1; i < n; i++)
       if (arr[i] > mx)
           mx = arr[i];
   return mx;
}

// A function to do counting sort of arr[] according to
// the digit represented by exp.
void countSort(int arr[], int n, int exp)
{
   int output[n]; // output array
   int i, count[10] = {0};

   // Store count of occurrences in count[]
   for (i = 0; i < n; i++)
       count[ (arr[i]/exp)%10 ]++;

   // Change count[i] so that count[i] now contains actual
   // position of this digit in output[]
   for (i = 1; i < 10; i++)
       count[i] += count[i - 1];

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

   // Copy the output array to arr[], so that arr[] now
   // contains sorted numbers according to current digit
   for (i = 0; i < n; i++)
       arr[i] = output[i];
}

// The main function to that sorts arr[] of size n using
// Radix Sort
void radixsort(int arr[], int n)
{
   // Find the maximum number to know number of digits
   int m = getMax(arr, n);

   // Do counting sort for every digit. Note that instead
   // of passing digit number, exp is passed. exp is 10^i
   // where i is current digit number
   for (int exp = 1; m/exp > 0; exp *= 10)
       countSort(arr, n, exp);
}

// A utility function to print an array
void print(int arr[], int n)
{
   for (int i = 0; i < n; i++)
       cout << arr[i] << " ";
}

// Driver program to test above functions
int main()
{
   int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
   int n = sizeof(arr)/sizeof(arr[0]);
   radixsort(arr, n);
   print(arr, n);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
IN C++: Create sets of 1 Million integers with the following characteristics; Sets where no numbers...
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
  • Create sets of integers with the following characteristics with array sizes of 100,000, 500,000 and 1000,000...

    Create sets of integers with the following characteristics with array sizes of 100,000, 500,000 and 1000,000 1. Set where no integer is repeated 2. Set where the range is low and the integers repeat Compare the performances of the following sorting algorithms. You can either write the algorithms on your own or modify algorithms obtained from elsewhere. Create a table with the rows being the different algorithms and the columns being the inputs. Run each input 10 times and report...

  • IN C++: Create sets of 1 Million integer, where no integer is repeated. Insert these numbers...

    IN C++: Create sets of 1 Million integer, where no integer is repeated. Insert these numbers to an (i) AVL tree and (ii) R-B tree. Compute the time taken to insert all the numbers. Repeat the experiment 10 times, each time regenerating the set. In a table report (a) the time taken to complete the insertion (b) the height of the tree, (c) the black height of the R-B Tree

  • Java 1. Create a 1D array of 4096 Unique random integer numbers of 1 to 5...

    Java 1. Create a 1D array of 4096 Unique random integer numbers of 1 to 5 digits. Of those numbers give me the following information: - Mean , Mode, Median, Range. - Five times, ask the user for a number within the range (from above) and record the time to find the number in the range. (Loop count) 2. Using a any sort algorithm, build sorted 2D array of random numbers (1 TO 5 digits ) with the x direction...

  • You are given an array of integers, where different integers may have different numbers of digits but the total number of digits over all integers in the array is n. Show how to sort the array in in...

    You are given an array of integers, where different integers may have different numbers of digits but the total number of digits over all integers in the array is n. Show how to sort the array in increasing order in O(n) time. Note: The number of integers in the array can be different for same value of n - for example the array with 2 integers 2468 and 12345 has 9 digits as well as the array with 5 integers...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • In C plus plus Objective: Create an array of numbers based upon user input. Program logic:...

    In C plus plus Objective: Create an array of numbers based upon user input. Program logic: Ask the user for how big to size the array. Create an array based upon that size. Ask for a number, add that number to the array. Repeat adding to the end until all numbers have been entered or until they enter -1 for the number. Print the list.

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • The main(int argc, char *argv[]) function will perform the following: • Evaluate whether value in argc...

    The main(int argc, char *argv[]) function will perform the following: • Evaluate whether value in argc is greater or equal to 3. • If it is not, then print the following message: Incorrect number of arguments - please call with assignment min max where assignment is the name of your executable and min/max define the range of numbers for your array. • If argc is ≥ 3 convert the values for min and max into integers and store them in...

  • Be sure to include the number of total swaps in each sort. Create a total_Time variable...

    Be sure to include the number of total swaps in each sort. Create a total_Time variable is the total time it takes all 1000 iterations to run. DO NOT INCLUE THE TIME IT TAKES TO GENERATE A UNIQUE RANDOM ARRAY EACH LOOP. In java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort and create and sort 1000 different arrays of this size,  timing the run to get...

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