Question

C++ Implement the radix sort of an array by using a queue for each group. The...

C++

Implement the radix sort of an array by using a queue for each group. The radix sort is discussed in Section 11.2.3 of Chapter 11 of your textbook. You can get more information in Radix sort.

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

Below is the solution:

#include <iostream>
#include <queue>
#include <ctime>
#include <cstdlib>
#include <math.h>
using namespace std;

void Sort_Array_Using_Radix(int q[], int n); //function prototype

int main() {
const int SIZE=100; //declare the Max size of the array
int array[SIZE]; //declare the variable array
int seed =time(0); //seed
srand(seed);
for (int i=0;i<SIZE;i++){ //loop SIZE times
      int num=0+rand()%SIZE; //generate the random number
      array[i]=num; //store the random number to arrray
}
for (int j=0;j<SIZE;j++) //loop SIZE times
      cout << array[j] << " "; //print the array value
cout << endl << endl;

Sort_Array_Using_Radix(array, SIZE); //call the function to sort the array using radix

//print the array after sorting
for (int j=0;j<SIZE;j++) //loop SIZE times
      cout << array[j] << " "; //print the array
cout << endl <<endl;
return 0;
}

//implement the function
void Sort_Array_Using_Radix(int q[], int n) {
    queue<int> bins[10]; //one array per possible digit in queue
    int maxDigits=3; //holds amount of digits in largest number
    int currentDigit=0; //starting base for decimal digit
    while (currentDigit < maxDigits) {
        for(int i=0; i<n; i++){ //loop n times
            int divisor = pow(10,currentDigit);
            int num = q[i]; //set to current value of array position
            int digitValue = static_cast<int>((num/divisor)%10); //get the decimal digit at current digit
            bins[digitValue].push(num); //put digits in corresponding bins
        }
        int i=0;
        for(int k=0;k<10;k++){ //loop through all bins
            while (!bins[k].empty()){ //push all elements in bin[k] until empty to a
                int temp=bins[k].front();
                q[i]=temp;
                bins[k].pop();
                i++;
            }
        }
        currentDigit++; //increment
    }
}

sample output:

39 51 37 57 85 80 0 83 0 59 45 41 3 55 4 12 50 27 18 19 56 78 35 69 27 27 15 93 71 9 68 10 61 58 67 98 38 67 82 91 79 27 84 34 82 88 99 84 67 17 56 75 48 43 45 27 22 60 21 93 69 41 4 82 99 23 81 90 43 15 81 74 42 17 8 76 57 7 13 25 77 21 52 25 64 97 4 86 9 25 32 31 67 36 13 18 11 46 8 6 

0 0 3 4 4 4 6 7 8 8 9 9 10 11 12 13 13 15 15 17 17 18 18 19 21 21 22 23 25 25 25 27 27 27 27 27 31 32 34 35 36 37 38 39 41 41 42 43 43 45 45 46 48 50 51 52 55 56 56 57 57 58 59 60 61 64 67 67 67 67 68 69 69 71 74 75 76 77 78 79 80 81 81 82 82 82 83 84 84 85 86 88 90 91 93 93 97 98 99 99 


Add a comment
Know the answer?
Add Answer to:
C++ Implement the radix sort of an array by using a queue for each group. The...
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
  • Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as...

    Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...

  • 5. Radix Sort Sort the following array using a radix sort with base 10, showing each...

    5. Radix Sort Sort the following array using a radix sort with base 10, showing each pass a. .791316.64|.39|.20|.89.53|.71.42 5. Radix Sort Sort the following array using a radix sort with base 10, showing each pass a. .791316.64|.39|.20|.89.53|.71.42

  • 5. Radix Sort Sort the following array using a radix sort with base 10, showing each...

    5. Radix Sort Sort the following array using a radix sort with base 10, showing each pass a. .791316.64|.39|.20|.89.53|.71.42

  • Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort....

    Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort. Verify the correctness of each implemented algorithm by sorting the following array: 10, 5, 60, 53, 45, 3, 25,37,39,48

  • c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations....

    c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations. Radix sort is a unique sorting algorithm. In this assignment, implement the Radix Sort algorithm, as explained the text book in chapter 2. Use the Numbers.txt file in the DataFiles folder for the numbers to sort. Extra credit is available for processing alphabetic strings instead of just numbers. Specification: * Using your Doubly-Linked list to create an dynamic array of Doubly-Linked lists (like lab1)....

  • Radix sort C++ Come up with an unsorted array of numbers (integer array). Sort the numbers...

    Radix sort C++ Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. USE THIS STUCTURE struct nodeQ{                            node *front;                            node *rear;               };               nodeQ que[10]; enqueue(que[i],data); EXAMPLE: Original, unsorted list: [170, 45, 75, 90, 2, 802, 24, 66] 1ST PASS:...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • Part 1: Extend your sorting framework with two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND (...

    Part 1: Extend your sorting framework with two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND (Merge Sort OR Quick Sort). If you choose Shell Sort, experiment with different incremental sequences to see how they affect the algorithm's run time efficiency (count the number of comparisons and exchanges). If you choose to implement Radix Sort, answer the following question as well: Can you write a version of Radix Sort to work with real numbers? If yes,...

  • 7. (14 points) Use LSD-first Radix Sort algorithm to sort the following array of numbers. Write...

    7. (14 points) Use LSD-first Radix Sort algorithm to sort the following array of numbers. Write the worst case, the best case time complexity and discuss if these sorting algorithms are stable and in-place? In what cases using these algorithms would not be efficient? (You must run Counting Sort for each digit explicitly.) A=[22,15,16,13,23,45,0,23,123]

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
Active Questions
ADVERTISEMENT