Question

In: Computer Science

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.

Solutions

Expert Solution

SOLUTION -

CODE -

//java code

import java.io.*;
import java.util.*;
import java.util.Arrays;
class Radix{
static 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;
}
  
static void countSort(int arr[], int n, int exp)
{
int output[] = new int[n]; // output array
int i;
int count[] = new int[10];
Arrays.fill(count,0);
  
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%10 ]++;
  
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
  
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
count[ (arr[i]/exp)%10 ]--;
}
  
for (i = 0; i < n; i++)
arr[i] = output[i];
}
  
static void radixsort(int arr[], int n)
{
int m = getMax(arr, n);
  
for (int exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp);
}
  
  
static void print(int arr[], int n)
{
for (int i=0; i<n; i++)
System.out.print(arr[i]+" ");
}
   static void reverse(int a[], int n)
{
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
  
/*printing the reversed array*/
for (int k = 0; k < n; k++) {
System.out.print(b[k]+" ");
}
}
  
  
  
public static void main (String[] args)
{
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = arr.length;
       System.out.println("Original Array");
       for (int i=0; i<n; i++)
System.out.print(arr[i]+" ");
       System.out.println("\nAscending Order ");
radixsort(arr, n);
print(arr, n);
       int arr1[] = {170, 45, 75, 90, 802, 24, 2, 66};
       System.out.println("\nOriginal Array");
       for (int i=0; i<n; i++)
System.out.print(arr1[i]+" ");
       System.out.println("\nDescending Order ");
radixsort(arr, n);
       reverse(arr, n);
   }
   }

output:

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

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...
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 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
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...
Hello i need someone to implement in JAVA a radix sort algorithm forsorting strings in ascending...
Hello i need someone to implement in JAVA a radix sort algorithm forsorting strings in ascending order. The input to your algorithm should be a (multi)set S = [S1, S2, . . . , Sn] of strings each of which is of length m over the English alphabet [A…Z, a…z]. The output should be the set sorted in ascending lexicographical order. Number of strings is >= 100 and number of characters >= 50 i need the answer fast please
// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
(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.
Hello i need someone to implement for me RADIX SORT IN JAVA for Characters NOT numbers...
Hello i need someone to implement for me RADIX SORT IN JAVA for Characters NOT numbers and i need the code super fast please
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT