Programming Assignment 4
CS 51520
Operating Systems
Spring, 2019

This assignment makes use of the files contained in this zip file. This assignment is due Friday, May 3.

The idea of this assignment is roughly this. Given a list of triples of positive integers, like this,

     (5 8 3)
     (5 8 1)
     (8 6 7)
     (6 1 1)
     (1 9 6)
     (4 3 1)
     (8 4 6)
     (7 9 3)
     (8 8 3)
     (4 1 5)

write code that sorts the list in each of the following ways.

  1. Sort the list in ascending order based on the sum of the first two elements of each triple.
  2. Sort the list in descending order based on the sum of the first two elements of each triple.
  3. Sort the list in ascending order based on the sum of the first and third elements of each triple.
  4. Sort the list in ascending order based on the maximum of the second and third elements of each triple.
  5. Sort the list in descending order based on the maximum of the second and third elements of each triple.
  6. Sort the list in ascending order based on the minimum of the first and second elements of each triple.
  7. Sort the list in ascending order based on the first element of each triple, and use the second element of a triple as a tie breaker.
  8. Sort the list in descending order based on the third element of each triple, and use the first element of a triple as a tie breaker.
  9. Sort the list in ascending order based on the first element of each triple, and use the second element of a triple, in descending order, as a tie breaker.
  10. Sort the list in ascending order based on the product of the first two elements of each triple, and use the third element of a triple as a tie breaker.

The real goal of the assignment is to implement these sorts in as little code as possible, hopefully by using carefully parameterized comparators. There are purposely a large number of sorts to try to force you to emphasize code reuse. (One obvious way to do this problem is to write ten different comparators, one for each sorting problem, but that will lead to a lot of redundant code.)

In the zip file there are solutions to this problem in three different languages, Java, Haskell, and Racket. The Java version is probably the one you will have the least trouble reading. The Haskell version is there because Rust takes a lot of ideas from Haskell, so the Rust solution may even end up looking more like Haskell than Java. The Racket version is in there because I wrote it several years ago, so why not include it.

Your Rust program needs to do three things. Generate a random list (or array) of integer three-tuples. Define a single "comparator factory function" that can be used to instantiate all ten of the needed comparators. Then sort the list of three-tuples ten times, using your comparator factory to generate a different comparator for each sort, and print the results.

Turn in a zip file called CS51520Hw4Surname.zip (where Surname is your last name) containing your version of triples.rs.

This assignment is due Friday, May 3.