You are going to apply RadixSort to sort vectors The input s

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.

Examples of Input and Output:

Input

5

3 3 3 3 3 2 2 2 2 2

2 3 2 2 2 2 2 2 2 2

1 3 0 0 2 1 0 0 0 0

1 3 0 0 2 2 0 0 0 0

2 3 2 1 2 2 2 2 2 2

Output
1;3;0;0;2;1;0;0;0;0;

1;3;0;0;2;2;0;0;0;0;

2;3;2;1;2;2;2;2;2;2;

2;3;2;2;2;2;2;2;2;2;

3;3;3;3;3;2;2;2;2;2;

Solution

source code:-

#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 ]--;
}

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

// Radix Sort
void radixsort(int arr[], int n)
{
// Find the maximum number to know number of digits
int m = getMax(arr, n);

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] << \" \";
}
int num;
int main() {
   int num,i,j,arr[10];
   printf(\"enter the number of vectors:\");
   scanf(\"%d\",&num);
   printf(\"enter vectors which have values between 0,1,2,3 and length equals to 10:\");
   for(j=0;j<num;j++)
   {
   scanf(\"%d\",&arr[j]);
  
   }
  
   int val = num/10;
radixsort(arr, val);
print(arr, val);
return 0;
  
}

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 follo
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 follo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site