Question

In: Computer Science

develop a  multithreaded version of radix sort c language

develop a  multithreaded version of radix sort


c language

Solutions

Expert Solution

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

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

// 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
   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];

   // 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 = getRax(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);
}

// 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;
}


Related Solutions

(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from the...
(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from the low order to high order. The input array is A = {329, 457, 657, 839, 436, 720, 353}
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
i) Pen down the algorithm for Radix sort. ii) What is the complexity of Radix sort?...
i) Pen down the algorithm for Radix sort. ii) What is the complexity of Radix sort? iii) Give any 2 real time examples of Radix sort.
i) Pen down the algorithm for Radix sort. ii) What is the complexity of Radix sort?...
i) Pen down the algorithm for Radix sort. ii) What is the complexity of Radix sort? iii) Give any 2 real time examples of Radix sort.
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718...
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads....
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads. The program will take in an array from 1 to n (n = 50) and will be passed to four different threads: If the current number is divisible by 2, then print HAP If the current number is divisible by 5, then print PY If the current number is divisible by both 2 and 5, then print HAPPY If the number is neither divisible...
Why is radix sort generally rejected by so many computer programmers? Under what conditions does Radix-Sort...
Why is radix sort generally rejected by so many computer programmers? Under what conditions does Radix-Sort run faster than O(n lg n) algorithms such as quicksort? Under what conditions does Radix-Sort run slower than O(n lg n) algorithms such as quicksort? How common are each of these conditions? When do you think a sort such as radix sort would be useful?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT