Question: Most of the sorting algorithms we studied so far are comparison - based sorting. That means these algorithms sort a list by comparing the elements
Most of the sorting algorithms we studied so far are comparisonbased sorting. That means these algorithms sort a list by comparing the elements to one another. The worstcase running time of a comparison sort cannot be lower than N log N where N is the number of inputs, as this is the minimum number of comparisons necessary to know where to put each element in the list.
There is a more efficient sorting algorithm for certain types of inputs. When a list contains elements from a certain range of values, it is possible to sort lists by counting the frequency of the elements.
In this lab, you will write a program that will count how many times an element appears in a list of numbers.
Input
The input will be in the following format:
In line you will be given the size say N of the list of numbers.
In line there will be N spaceseparated numbers that belong to the list.
Output
Your output will be the number of times each number appears on the list.
Sample Input
Sample output
Explanation
The output prints the number of times each number appears in the list. appears times, appears time, appears times, etc.
CountingSort.java
import java.util.;
public class CountingSort
static int countingSortint array
Complete this method
public static void mainString args
Scanner in new ScannerSystemin;
Read the number of inputs
int n innextInt;
int array new intn;
Read the inputs into an array
forint i ; i n; i
arrayi innextInt;
result will contain the counts of the numebrs
int result countingSortarray;
for int i ; i result.length; i
System.out.printresultii result.length : ;
System.out.println;
inclose;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
