Question

In: Computer Science

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

Radix sort
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.

C++ program
thanks

Solutions

Expert Solution


/*
 *  C++ Program for radix sort ascending and descending
 */

#include <bits/stdc++.h>
using namespace std;
#define MAX 50

void RadixSortAsc(int a[], int n)
{
  int i, m = 0, exp = 1, b[MAX];
  
  for (i = 0; i < n; i++)
  {
    if (a[i] > m)
      m = a[i];
  }
  
  while (m/exp>0)
  {
    int bucket[10] = {0};
    for (i = 0; i < n; i++)
      bucket[a[i]/exp%10]++;
    for (i = 1; i < 10; i++)
      bucket[i] += bucket[i-1];
    for (i = n-1; i >= 0; i--)
      b[--bucket[a[i]/exp%10]] = a[i];
    for (i = 0; i < n; i++){
      a[i] = b[i];
    }
    exp *= 10;
  }
}

void RadixSortDesc(int a[], int n)
{
  int i, m = 0, exp = 1, b[MAX];
  
  for (i = 0; i < n; i++)
  {
    if (a[i] > m)
      m=a[i];
  }
  
  while (m/exp>0)
  {
    int bucket[10] = {0};
    for (i = 0; i < n; i++)
      bucket[9-a[i]/exp%10]++;              // this line is changed for desc
    for (i = 1; i < 10; i++)
      bucket[i] += bucket[i-1];
    for (i = n-1; i >= 0; i--)
      b[--bucket[9-a[i]/exp%10]] = a[i];    // this line is changed for desc
    for (i = 0; i < n; i++){
      a[i] = b[i];                          // this line is changed for desc
    }
    exp *= 10;
  }
}

void print(int arr[], int n)
{
  for (int i = 0; i < n; i++)
    cout << arr[i] << " ";
}

int main()
{
  int arr[] = {70, 40, 50, 90, 10, 20, 0, 60};
  int n = sizeof(arr)/sizeof(arr[0]);

  cout << "Before -" << endl;
  print(arr, n);
  cout << endl;

  cout << "Ascending Sort -" << endl;
  RadixSortAsc(arr, n);
  print(arr, n);
  cout << endl;

  cout << "Descending Sort -" << endl;
  RadixSortDesc(arr, n);
  print(arr, n);
  cout << endl;
  
  return 0;
}


Related Solutions

Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort 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. (Write a C# program)
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome 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. I need this in java language.
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome 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. I need this in java language. Radix Sort assignment (CISP430 ignore this part) This is only for those who are using Java How many classes do I need? A: Node, Queue, Radix, Driver...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
(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}
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
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...
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?
Give an example of how Radix sort works
Give an example of how Radix sort works
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort,...
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT