CISC 1115
Introduction to Programming Using Java
Lab #15
Sorting and Searching

How to Develop and Submit your Labs

Lab 15.1 — Sorting Parallel Arrays (Optional)

Given the file 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.

Suggested Methods

While you are free to code this lab as you wish, I would recommend the following methods:
  • The usual 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.
  • Two sort methods, both accepting the two arrays and numbers of entries in the arrays (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
  • Other Suggestions

    Sample Test Run

    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.9
    
    the 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
    
    
  • Lab 15.2 — Largest Element Selection Sort (Optional)

    The selection sort presented in class repeatedly found smallest elements of the remaining values to be sorted and placed them in successive positions beginning at 0 and moving forward through the array. An equivalent approach is to successively find the largest elements and place them in the array beginning at the last element and moving backward through the array.

    Code this second version of selection sort.

    Sample Test Run

    For example if the file numbers.text contains:

    6
    2
    6
    3
    4
    1
    5
    
    the 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}