Question

Please use counting sort.

RadixSort In this assignment you will im Input structure You are going to apply RADIXSORT to sort vectors. The input starts with an integer number which indicates the number of vectors to be sorted. Then vectors follow, one vector per line. Each vector consists of 10 digits where each digit has a value in 0...9. Entries of a vector are separated by a space Output structure Output the sorted sequence of vectors, one per line. Vector i must appear before vector j in your output if and only if for some d E ,2,..., 10), vector i is smaller than or equal to vector j on the dth entry, and the two vectors are equal for any of the first d-1 entries.

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

I would be providing my solution in C++ as it was not asked for a particular language to implement RADIX SORT.

The idea is to sort digit by digit starting from the least significant digit and moving to the most significant digit. Here counting-sort is used as a subroutine to sort.

Here is the Code:

#include <bits/stdc++.h>

using namespace std;

void countSort(vector<int> v[],int n,int dig)

{ // A function to do counting sort of array of vector according to the digit represented by dig.

vector<int> out[n]; // output array of vector

int i;

int count[10] = {0};

for (i = 0; i < n; i++){  

vector<int> x=v[i];// Store count of occurrences in count[]

int c=x[dig];//extract the digit of the vector v[i] at 'dig' position

count[c]++;

}

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

{

count[i] += count[i - 1];

}

for (i = n - 1; i >= 0; i--)// Construct the output array of vector

{

out[count[ v[i][dig] ] - 1] = v[i];//read carefully how the index is manipulated relating arr[], ccount, output.

count[ v[i][dig] ]--;

}

for (i = 0; i < n; i++)// Copy the output(out) array of vector to v[] so that v[] now contains sorted numbers

//according to current digit

{  

v[i] = out[i];

}

}

void RadixSort(vector<int> v[],int n)

{ //The idea is to sort digit by digit starting from the least significant digit and moving to the most significant digit.   

for (int i = 9;i>=0; i--)//Here counting-sort is used as a subroutine to sort.

//We have no of digits =10 and 10th digit is least significant digit

{

countSort(v,n,i);//passing ith digit to solve in v vector

}

}

void print_vector(vector<int> v[],int n)

{

int i,j;

for(i=0;i<n;i++)

{

for(j=0;j<v[i].size();j++)

{

cout<<v[i][j];

}

cout<<"\n";

}

}

int main() {

int n;

cin>>n;//No of Vectors to Sort

int i,j;

vector<int> v[n+1];//To store n vectors declare array of vectors

for(i=0;i<n;i++)

{

for(j=0;j<10;j++)//Each vector will store 10 digit mentioned in the input structure

{

int c;

cin>>c;

v[i].push_back(c);//push the required digit in required vector

}

}

cout<<"Array of Vector Before Sorting"<<"\n";

print_vector(v,n);//print vector before sorting

RadixSort(v,n);//Radix Sort

cout<<"Array of Vector After Sorting"<<"\n";

print_vector(v,n);

return 0;

}

random.cpp 1 #include <bits/stdc++.h» 3 void countsort (vectorcint> vl,int n,int dig) untitled 2 using namespace std 4 // A function to do counting sort of array of vector according to the digit represented by dig vector<int> out[n];// output array of vector int i int count[10] (o); for (1-0; 1 < n; i++){ 6 8 9 10 vector<int x-vi];// Store count of occurrences in count int c-x[dig];//extract the digit of the vector v[i] at dig position count [c]++; 12 13 14 for (İ -1; İ く 10; İ++) // Change count[i] so that count [í] now contains actual position of this digit in output[] count[i] count [i 1]; 16 17 18 19 20 21 for (i-n -1; i >-e; i-)// Construct the output array of vector out [count v[i][dig] ] - 1] v[i];//read carefully how the index is manipulated relating arr[], scount, output countl v[i][dig] ]-; 23 24 25 26 27 28 29 void Radixsort (vector<int> v[l,int n) 3 /The idea is to sort digit by digit starting from the least significant digit and moving to the most significant digit 31 32 for (i = 0; i < n; i++)// copy the output (out) array of vector to v[] so that v[] now contains sorted numbers //according to current digit v[i] out[i]; for (int i - 9;1>-0; i//Here counting-sort is used as a subroutine to sort //We have no of digits -10 and 10th digit is least significant digit countsort(v,n,i);//passing ith digit to solve in v vector 34 35 36 37 void print_vector(vector<ints vl,int n) 38Input:

5
4 3 4 1 5 6 7 7 8 5
2 3 4 1 5 6 7 7 8 9
4 3 4 1 5 6 7 7 8 6
8 3 4 1 5 6 7 7 8 6
4 3 4 1 5 6 7 9 8 4

Output:

Array of Vector After Sorting
2341567789
4341567785
4341567786
4341567984
8341567786

Thanks and Please Upvote

Add a comment
Know the answer?
Add Answer to:
Please use counting sort. RadixSort In this assignment you will im Input structure You are going...
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
  • Instruction: In C++ you are going to apply RadixSort to sort vectors. The input starts with...

    Instruction: In C++ you are going to apply RadixSort to sort vectors. The input starts with an integer number which indicates the number of vectors to be sorted. Then vectors follow, one vector per line. Each vector consists of 10 numbers where each number has a value in {0, 1, 2, 3}. Entries of a vector are separated by a space. NOTE: READ THE INSTRUCTIONS PLEASE. DO NOT JUST COPY & PASTE CODE. Examples of input and output Input 3333322222...

  • 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...

  • PROGRAM DESCRIPTION Implement the combined O(n) radix/bucket sort as described in class. (i.e. divide the input...

    PROGRAM DESCRIPTION Implement the combined O(n) radix/bucket sort as described in class. (i.e. divide the input by radix, bucket sort (with no insertion sort step) once for each radix starting from the least significant. Make sure that your overall implementation is O(n) NPUT The input to your program will an unspecified number of entries. Each entry is a non-negative integer containing nine (zero padded) digits ( this means that the integer may have either leading or trailing zeros), one per...

  • in C++ HeapSort Implement the heapsort algorithm for sorting an array of integers. Input structure Each...

    in C++ HeapSort Implement the heapsort algorithm for sorting an array of integers. Input structure Each case starts with an integer number which indicates the number of elements to be sorted. Then, the elements follow, one per line. You can assume the input is correctly structured (i.e., no data are missing Output structure Output the sorted sequence separeted by";" (in non-decreasing order). Do not insert spaces or a new line at the beginning or at the end of any element.

  • in C++ Description In this lab assignment your job is to implement Merge-Sort. See the textbook...

    in C++ Description In this lab assignment your job is to implement Merge-Sort. See the textbook for the algorithm and its pseudo-code. You must output the given elements integers) in non- decreasing order. Input structure The input starts with an integer number which indicates the number of elements, n. Then, the elements follow, one per line. Output structure Output the elements in non-decreasing order. Each element must be fol- lowed by ;. Examples of input and output: Input ܗ ܗ...

  • 5.35 LAB: Sort a vector Write a program that gets a list of integers from input,...

    5.35 LAB: Sort a vector Write a program that gets a list of integers from input, and outputs the integers in ascending order (lowest to highest). The first integer indicates how many numbers are in the list. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 10 4 39 12 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space, including the last...

  • IN PYTHON 3: In this version of Radix Sort we use Queues very naturally. Let us...

    IN PYTHON 3: In this version of Radix Sort we use Queues very naturally. Let us consider the following set of positive integers: 311, 96, 495, 137, 158, 84, 145, 63 We will sort these numbers with three passes. The number of passes is dependent on the number of digits of the largest number - in this case it is 495. In the first pass we will go through and sort the numbers according to the digits in the units...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • C programming question please meet all of the following conditions don't just output the same answer ​​​​​​​ Given a...

    C programming question please meet all of the following conditions don't just output the same answer ​​​​​​​ Given a sequence a. The elements of a has the following infomations: index The index is followed by the input order, starts from 1. The input won't contain index , you have to record it yourself. Smaller input index has higher priority. admin level : Level starts from 0 to 999. level 0 has the highest priority, while level 999 has the lowest....

  • In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of...

    In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of the String class. The strings are provided as arguments to your main method. In case you haven't yet learned to configure command-line arguments to main in your IDE, now is a good time to learn. Your program should sort the array of strings using mergesort, then print the strings one per line, in order from smallest ("a") to largest ("z"). The name of your...

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