Write a program that sorts a range of integers typed into th
Write a program that sorts a range of integers typed into the console using the counting sort algorithm2. Values typed that are not in the range 1-100 (inclusive) should be ignored. You must prompt the user to enter the numbers. Then, print out the numbers entered in sorted order (on a single line, with spaces between subsequent values). Similar to the prior problems, the hasNextInt() method of the scanner will be useful, and when testing in Eclipse, press enter and then, once on the empty line, press CTRL-D (Mac) or CTRL+Z (Windows) when you are done typing numbers.
You must use an array to keep track of how many times each number has been seen. The array should have 100 elements (one for each number). Index 0 should be used to track the number of 1’s, index 1 to track the 2’s, index 2 to track the 3’s, etc., up to index 99 for the 100’s. You could use a massive if/else block, but the whole reason to use arrays is to make your programs easier. So, instead, think about how to convert each number you read into the correct index and then increment that value in the array (this should be similar to, but simpler than, Problem b).
Here is an example run of the program:
Enter numbers from 1-100: 1 9 2 100 99 98 90 10
1 2 9 10 90 98 99 100
You have been supplied JUnit tests for several example input sequences, including an empty one, one with numbers not in the valid range (i.e. outside 1-100), one with a single valid number, one with the numbers already in order (both with/without duplicates), and one with multiple numbers out of order.
Solution
Please follow the code and comments for description :
CODE :
import java.util.ArrayList; // required imports
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Scanner;
public class SortInput { // class to run the code
public static void main(String[] args) { // driver method
 System.out.println(\"Please Enter the Numbers form 1 to 100 : \"); // prompt to enter the data
 Scanner sc = new Scanner(System.in); // scanner class
 ArrayList lineData = new ArrayList(); // required initialisations
 int occCount[] = new int[100];
 StringBuffer sb = new StringBuffer();
lineData.add(sc.nextInt()); // add the data
 while (sc.hasNextInt()) { // check for the new line
 lineData.add(sc.nextInt());
 }
 Collections.sort(lineData); // sort the data
 for (int i = 0; i < lineData.size(); i++) { // iterate over the loop of data
 sb.append(lineData.get(i)).append(\" \"); // append the data to the buffer with a space
 }
 System.out.println(\"The Sorted Data is : \" + sb.toString()); // print the data to the console
 for (int j = 1; j <= 100; j++) {
 int occurrences = Collections.frequency(lineData, j); // count the occurrences
 occCount[j - 1] = occurrences;
 }
 System.out.println(Arrays.toString(occCount)); // print to console
 }
 }
OUTPUT :
Please Enter the Numbers form 1 to 100 :
 1
 2
 3
 4
 5
 36
 1
 11
 2
 25
 35
 47
 88
 94
 34
 54
 72
 12
 21
 1
 The Sorted Data is : 1 1 1 2 2 3 4 5 11 12 21 25 34 35 36 47 54 72 88 94
 [3, 2, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
 Hope this is helpful.


