roster.text
, containing pairs of ids (integers) and gpa's (doubles), read the file into a pair of arrays, and perform two sorts on them, the first by name in
ascending order, the second by gpa in descending order. Print out the original and sorted arrays.
read
method … accepts either the filename or a Scanner
object created by main
, together
with the arrays and the capacity. The method loads up the arrays returns the number of entries read in.
size
). Since the two methods have the exact same signature,
they cannot be overload (i.e., use the exact same name) … I used the named sortById
and sortByGPA
swap
accepts an array and a pair of indexes and swaps the values at those two indices (look at Lecture 12 for details on swapping elements of an array).
You need two of these methods, one for swapping the elements of the id (int) array and the other for the gpa (double) array. Since these two methods will have different
signatures (i.e., their paraeter lists are not identcal), you can use the same name for both (I used swap
).
print
accepting bot arrays and the size and prints them out in the appropriate manner
swap
code from Lecture 12
read
methods from the notes or the solutions to the sample finals
sortBy
and swap
. Write one and copy to the other.
A word of caution, make sure you also understand the code you are copying; it's often too easy t copy without understanding the code, and that sort of defeats the purpose of doing these exercises.
For example if the file roster.text
contains:
10050 3.1 10721 2.3 10010 3.7 30921 2.5 23462 4.0 12345 2.9the program should produce the following output:
Original data: 10050: 3.1 10721: 2.3 10010: 3.7 30921: 2.5 23462: 4.0 12345: 2.9 Sorted by Id: 10010: 3.7 10050: 3.1 10721: 2.3 12345: 2.9 23462: 4.0 30921: 2.5 Reverse Sorted by GPA: 23462: 4.0 10010: 3.7 10050: 3.1 12345: 2.9 30921: 2.5 10721: 2.3
Code this second version of selection sort.
SelectionSort
of the corresponding lecture).
numbers.text
(assume a header value precedes the numbers).
Sample Test Run
For example if the file numbers.text
contains:
6 2 6 3 4 1 5the program should produce the following output:
Initial: {2, 6, 3, 4, 1, 5} Pass #1: {2, 5, 3, 4, 1, 6} Pass #2: {2, 1, 3, 4, 5, 6} Pass #3: {2, 1, 3, 4, 5, 6} Pass #4: {2, 1, 3, 4, 5, 6} Pass #5: {1, 2, 3, 4, 5, 6} Pass #6: {1, 2, 3, 4, 5, 6} Sorted: {1, 2, 3, 4, 5, 6}