Question

JAVA PLEASE Using queues Radix sort Come up with an unsorted array of numbers (integer array)....

JAVA PLEASE
Using queues
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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution =>

import java.util.*;
class Radix_Sort {
public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in); //if you want to input array by yourself
int[] a = {96,23,109,45,65,23,67,89,39,11};
int[] b = a;
radix_sort_asc(a);
System.out.println("\n The sorted array in ascending order is: \n");
for(i=0;i<10;i++)
System.out.print(a[i] + " ");
System.out.println();
a = b; // resetting the array
radix_sort_desc(a);
System.out.println("\n The sorted array in descending order is: \n");
for(i=0;i<10;i++)
System.out.print(a[i]+ " ");
}
  
static int largest(int a[])
{   
int larger=a[0], i;   
for(i=1;i<10;i++)
{
if(a[i]>larger)
larger = a[i];
}
return larger;
}
static void radix_sort_asc(int a[]) // ascending order radix sort function
{
int n = a.length;
int MAX = largest(a);
int i, m=0, exp=1;
int b[] = new int[MAX];
for (i=0; i<n; i++)
{
if (a[i]>m)
m=a[i];
}
while (m/exp>0)
{
int bucket[]=new int[10];
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;
}
}
static void radix_sort_desc(int a[]) // Descending order radix sort function
{int n = a.length;
int MAX = largest(a);
int i, m=0, exp=1;
int b[] = new int[MAX];
for (i=0; i<n; i++)
if (a[i]>m)
m=a[i];
while (m/exp>0)
{
int bucket[]=new int[10];
for (i=0; i<n; i++)
bucket[9-a[i]/exp%10]++; // changed this line
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]; // changed this line
for (i=0; i<n;i++){
a[i]=b[i]; // changed this line
}
exp*=10;
}
}
  
}

Add a comment
Know the answer?
Add Answer to:
JAVA PLEASE Using queues Radix sort Come up with an unsorted array of numbers (integer array)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Radix sort C++ Come up with an unsorted array of numbers (integer array). Sort the numbers...

    Radix sort C++ 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. USE THIS STUCTURE struct nodeQ{                            node *front;                            node *rear;               };               nodeQ que[10]; enqueue(que[i],data); EXAMPLE: Original, unsorted list: [170, 45, 75, 90, 2, 802, 24, 66] 1ST PASS:...

  • in java Create an Eclipse Java project that utilize "Selection Sort" to sort an unsorted integer...

    in java Create an Eclipse Java project that utilize "Selection Sort" to sort an unsorted integer array of size 20 of random numbers in the range 0-500 inclusive. The main method should display the unsorted array and sorted array after Selection Sort.

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as...

    Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...

  • Please come up with an array of 9 random integers then sort the array. Show the...

    Please come up with an array of 9 random integers then sort the array. Show the contents of the array each time a sorting algorithm changes it while sorting the array into ascending order. The 6 sorting algorithm we are using are: 1. Selection Sort 3 points 2. Insertion Sort 3 points 3. Shell Sort 6 points 4. Bubble Sort 3 points 5. Merge Sort 10 points 6. Quick Sort 10 points It is OK to do this assignment on...

  • please do by java P14.6 Implement the radix sort algorithm described in Exercise R14.22 (below) to...

    please do by java P14.6 Implement the radix sort algorithm described in Exercise R14.22 (below) to sort arrays of numbers between 0 and 999. P14.7 Implement the radix sort algorithm described in Exercise R14.22 (below) to sort arrays of numbers between 0 and 999. However, use a single auxiliary array, not ten. .P14.8 Implement the radix sort algorithm described in Exercise R14.22 (below) to sort arbitrary int values (positive or negative

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • In Java Create an array of Student objects. Display the students by age in UNSORTED order....

    In Java Create an array of Student objects. Display the students by age in UNSORTED order. Apply the Quick sort and Display the students by age in SORTED order. public class TestQuickStudent { public static void main (String [ ] args ) { /* ***Create your array of Student objects with AT LEAST 5 names */ System.out.println ( " ____________________________");    /* ***LOOP through your array and print out each UNSORTED check by student age */ System.out.println ( " ____________________________");...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • 7. (14 points) Use LSD-first Radix Sort algorithm to sort the following array of numbers. Write...

    7. (14 points) Use LSD-first Radix Sort algorithm to sort the following array of numbers. Write the worst case, the best case time complexity and discuss if these sorting algorithms are stable and in-place? In what cases using these algorithms would not be efficient? (You must run Counting Sort for each digit explicitly.) A=[22,15,16,13,23,45,0,23,123]

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT