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. 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 Do not use any of the built-in data structures What goes into the Radix class A: There are a couple of ways to do this. You can deal with instances or you can make everything in this class static. The static approach makes the most sense For the static approach, you can simply call a static method inside the Radix class as such Radix.sort(arr); where arr represents the array to be sorted. This function can return a sorted array. The sort method can call helper functions so that it doesn’t become too long. Where should I put the functions that display the array in ascending and descending order? A: It is up to you. This can be done in the Driver Class or it can be done in the Radix class General: Why do I need a queue A: You need an array of queues( from 0-9) which correspond to all the possible digits.For example queue[0] will hold all the numbers that contain a zero in the spot that corresponds to the pass that you are on. For example, let’s say we have 120, 30,41 For pass 1, Queue[0] would contain 120 and 30 Queue[1] would contain 41 Notice on the 1st pass, we are processing the 1st digit from the right and on each subsequent pass, we would work our way to the right one digit at a time. To display in descending order, can I just display the array backwards A: yes Do I worry about negative numbers? A: You don’t have to incorporate negative numbers for this lab Where do I define my array and how big does it have to be? A: You can hardcode your array in your program and make it as big or small as you want it.

Solutions

Expert Solution

The java code for radix sort is:

public class radixSort{
        static void occur_ascending(int arr[], int n, int j)
        {
                int temp[] = new int[n];
                int count[] = new int[10];
                for (int i = 0; i < n; i++) // no. of occurrences is stored
                        count[(arr[i] / j) % 10]++;
                for (int i = 1; i < 10; i++)
                        count[i] += count[i - 1];
                for (int i = n - 1; i >= 0; i--) {
                        temp[count[(arr[i] / j) % 10] - 1] = arr[i];
                        count[(arr[i] / j) % 10]--;
                }
                for (int i = 0; i < n; i++) // store the modified array after sorting according to digit's place
                        arr[i] = temp[i];
        }
        static void occur_descending(int arr[], int n, int j)
        {
                int temp[] = new int[n];
                int count[] = new int[10];
                for (int i = 0; i < n; i++) // no. of occurrences is stored
                        count[9-(arr[i] / j) % 10]++;
                for (int i = 1; i < 10; i++)
                        count[i] += count[i - 1];
                for (int i = n - 1; i >= 0; i--)
                        temp[--count[9-(arr[i] / j) % 10]] = arr[i];
                for (int i = 0; i < n; i++) // store the modified array after sorting according to digit's place
                        arr[i] = temp[i];
        }
        public static void main(String[] args)
        {
                int arr1[] = {23, 56, 9, 198, 30, 1, 5203, 601}; // random array given as input
                int n = arr1.length; // length of array is found
                int a = arr1[0]; // maximum element of array is found
                for (int i = 1; i < n; i++)
                        if (arr1[i] > a)
                                a = arr1[i];
                for (int j = 1; a / j > 0; j *= 10)
                        occur_ascending(arr1, n, j);
                for (int i = 0; i < n; i++)
                        System.out.println(arr1[i]);
                System.out.println("");
                int arr2[] = {23, 56, 9, 198, 30, 1, 5203, 601}; // random array given as input
                n = arr2.length; // length of array is found
                a = arr2[0]; // maximum element of array is found
                for (int i = 1; i < n; i++)
                        if (arr2[i] > a)
                                a = arr2[i];
                for (int j = 1; a / j > 0; j *= 10)
                        occur_descending(arr2, n, j);
                for (int i = 0; i < n; i++)
                        System.out.println(arr2[i]);
        }
} 

The sample of the code is:

The output of the code is:

I have ignored the part you have mentioned in the above question.


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 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)
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
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.
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
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
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT