Question

In: Computer Science

Use counting sort, sort the following numbers: 4, 2, 5, 4, 2, 3, 0, 2, 4,...

Use counting sort, sort the following numbers: 4, 2, 5, 4, 2, 3, 0, 2, 4, 3

Solutions

Expert Solution

Code:

import java.util.*;
public class Main
{
        public static void main(String[] args) 
        {
                int a[]=new int[10];
                int c[]=new int[10];
                int i,j,k;
                Scanner sc=new Scanner(System.in);
            System.out.println("Enter numbers::");
            for(i=0;i<10;i++)
            {
                a[i]=sc.nextInt();
                /*Assign each element Array C with 0*/
                c[i]=0;
            }
            for(i=0;i<10;i++)
            {
                /* Read each elemenet of array A and assign it to k*/
                k=a[i];
                /*Increment c[k] by 1*/
                c[k]=c[k]+1;
            }
            System.out.println("--After Sorting--");
            for(i=0;i<10;i++)
            {
                for(j=0;j<c[i];j++)
                {
                    System.out.print(i+" ");
                }
            }
        }
}

Screenshots:


Related Solutions

Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1,...
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1, 0, 2, 1, 5, 0  
Consider the following set of numbers: {3, 5, 2, 5, 5, 15, 2, 2, 4, 4,...
Consider the following set of numbers: {3, 5, 2, 5, 5, 15, 2, 2, 4, 4, 20, 4998, 4} 14. The Q3 of this set is: a. 4 b. 5 c. 2 d. 10 15. The Q1 of this set is: a. 4 b. 5 c. 2 d. 2.5 e. 3
Use these cash flows to answer the following questions: Year 0 1 2 3 4 5...
Use these cash flows to answer the following questions: Year 0 1 2 3 4 5 6 A (4,000) 800 1,400 1,300 1,200 1,100 1,000 B (2,000) 700 1,300 1,200 0 0 0 a. Calculate the NPV for projects A and B using a 20 percent discount rate (write the calculate keys that you use to get the answer). b Calculate the Equivalent Annual Annuity (EAA) for projects A and B using a 20 percent discount rate (write the calculate...
Recall that hexadecimal numbers are constructed using the 16 digits 0, 1, 2, 3, 4, 5,...
Recall that hexadecimal numbers are constructed using the 16 digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. (a) How many strings of hexadecimal digits consist of from one through four digits? (b) How many strings of hexadecimal digits consist of from two through five digits?
SHOW WORK Sort the given keys using Counting sort algorithm. Also write the algorithm.          5, 2,...
SHOW WORK Sort the given keys using Counting sort algorithm. Also write the algorithm.          5, 2, 3, 1, 0, 2, 1, 5, 0                                                                  SHOW WORK!
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6...
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6 1 5 6 8 2 4 6 7 3 4 5 9 10 5 8 9 4 7 9 6 7 8 6 How can I detect when 'cin' starts reading from a new line. The amount of numbers in each row is unknown. I need them in type 'int' to use the data.
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5 3 0 2 5 4 1 6 9 5 3 1 2 10 6 1 1 2 1 19 6 6 6 7 0 4 1 1 1 0 1 9 2 2 2 1 16 10 10 5 2 3 1 4 4 4 3 6 2 8 5 2 7 1 6 4 0 3 1 1 1 Background: A group of...
Given the following dataset x   1   1   2   3   4   5 y   0   2   4   5  ...
Given the following dataset x   1   1   2   3   4   5 y   0   2   4   5   5   3 We want to test the claim that there is a correlation between xand y. The level of cretaine phosphokinase (CPK) in blood samples measures the amount of muscle damage for athletes. At Jock State University, the level of CPK was determined for each of 25 football players and 15 soccer players before and after practice. The two groups of athletes are trained...
Given the following dataset x 1 1 2 3 4 5 y 0 2 4 5...
Given the following dataset x 1 1 2 3 4 5 y 0 2 4 5 5 3 We want to test the claim that there is a correlation between xand y. (a) What is the null hypothesis Ho and the alternative hypothesis H1? (b) Using α= 0.05, will you reject Ho? Justify your answer by using a p-value. (c) Base on your answer in part (b), is there evidence to support the claim? (d) Find r, the linear correlation...
Use the Data Below to answer the following question below 0 0 2 0 5 3...
Use the Data Below to answer the following question below 0 0 2 0 5 3 1 12 0 0 0 1 6 0 1 1 2 8 1 3 1 6 2 4 0 16 17 0 8 0 3 0 0 1 2 5 2 0 2 1 5 0 7 0 1 0 0 1 0 0 3 1 9 4 1 3 0 1 1 1 0 7 1 9 2 0 1 1 1 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT