Due: Friday, January 29, 1999
Read a list of positive and negative integers from the standard input and print them out from least to greatest.
If the input is:99 456 12 9876then, the output of the program should be:
12 99 456 9876You may assume that each number will be on a line by itself and that the input will have less than 1000 lines. Be sure to sort as efficiently as reasonable (getting a sort algorithm from a book might be a good idea). The output must have the same number of lines as the input. You do not need to check if the line is a properly formed integer; just use parseInt() as illustrated below.
Your program is required to implement two procedures (static methods) sort and compare. The procedure sort must be called once to sort the array, and the procedure compare must be called by sort every time a comparision of integers is made.
Use
java -Xrunhprof:cpu=times SortThen examine the file java.hprof.txt to see how efficient your program is.
Do not use the java.util package.
The basic structure in Java for converting a string to an integer is like this:
try { i = Integer.parseInt (s); // no white space in "s" allowed } catch (NumberFormatException e) { System.out.println ("The string \"" + s + "\"" is not an integer."); i = Integer.MIN_VALUE; // set "i" to something anyway }
To declare an array:
int [] A = new int [47]
IO in Java is complex. Use the Read_11 program as an example of how to read lines (strings) from the standard input.
Turn in the Java source code for the program. Use the following command on malestrom.cs.fit.edu or zach.cs.fit.edu
~ryan/bin/mfiles ryan@cs "cse4510/lab03" Sort.javaThe name of the file must be Sort.java. Be sure to put your name in comments somewhere at the beginning of the file. You may turn in the assignment more than once. All but the last submission will be ignored. With luck the submissions for this lab exercise will be found in the file lab03-sub.txt.