Lab 7 - Due Tuesday April 13

In this lab we start to look in more detail at sorting methods, and in particular how sorting methods relate to data structures.  In this lab we introduce the bin sort.  The best way to understand how a bin sort works is through an example.  Imagine that for each student in the class we have a score in the range from 0 to 100.  Our goal is to have a sorted list where the sort is done according to the score.  One way to quickly sort the list of students is by making a series of bins, one for each score (in this case there would be 101 total bins).  Figure 1 shows a simplified version of the algorithm where the student names are only one letter long and there are only 5 possible scores.

A natural implementation of a bin is a list.  As students are read in they are simply added to the appropriate list in a fashion similar to what was done in Lab 4.  To get a sorted list back, the bins are simply concatenated in order (from 0 to 100).

Furthermore, since all of the bins are seperate collections of data, these can be sorted as well.  So, for example, it would be reasonable to do an additional bin sort pass on each of the bins.  In the second pass the bins might correspond to the first letter of each student's names (for a total of 26 bins).  An alternative is to take the concatenated version of the entire list, and do bin sort passes on each section (eg. if there were 5 students who got 0's, the first bin sort pass would be on the first 5 elements of the list, etc.).
 

Task

You are given a pile of n cards.  Each card has three fields:  deck number, suit, and face value.  For purposes of this lab you can assume that all are represented as integers.  Since each deck has at most 52 cards (some may be missing), the pile has at most 52n cards.  You can assume that there is at least one card from each deck.  So the number of cards in the pile is at least n.

Your goal for this lab is to sort the pile through the use of bin sorting.  You should sort the pile by deck number, and within a deck number by suit, and within a suit by face value.  You will need three bin sort passes to accomplish this.  Since you know there are 4 suits and 13 face values, you will know the number of bins necessary for each of those phases.  You will not, however, be able to assume n.  It should be input to the program.

Your program should read in a file, the first line of which is n, and subsequent lines of which are the cards in the pile (one for each line).  The format of the cards in the pile will be deck suit face.  Your program should then sort the cards as described above and then print them out in order.  When outputting the cards make the following conversions for suits:  1 - clubs, 2 - diamonds, 3 - hearts, 4 - spades.  For face values:  11 - Jack, 12 - Queen, 13 - King, 1 - Ace.
 

Plan of attack

Your first goal in the assignment should be simple:  read in the cards from the input file and put them into a suitable data structure.  Your next goal should be to sort the data structure using a single bin sort pass.  On each pass through the data structure you should remove the elements one at a time, and put them into the appropriate bins.  Once you are done, then you can go through the bins one at a time to remake the data structure in the new, sorted fashion.  Once that is accomplished you can consider how the subsequent passes should proceed.

Debugging strategies will be crucial for this program. I highly reccommend getting reasonable display functions up and running as soon as possible. When you are debugging you will need to create suitable data to test your program on yourself. A good strategy would be to start with very small files and work your way up to more complicated ones.
 

Design Elements

There are several design aspects to consider in this program.  First is the issue of how to store each card.  A simple structure, with fields for deck, suit and face value should suffice.  Second, is the issue of the main data structure.  Some type of list is appropriate.  Consider the operations done in the bin sort.  You will go through the list one element at a time, transferring the element to bins.  Later you will collect them up in a similar fashion.  Also consider the space complexity.  Finally, there is the issue of the bins.  At any given time you will need x bins, where x is either n, 13, or 4.  Each bin should be some type of list.  One of your goals in this program should be to make the program efficient in terms of time and space complexity.  Among other things this means that you shouldn't have multiple copies of the same card floating around.  In short, your data structures should clean up after themselves, and they shouldn't use excessive memory in the first place (eg. don't simply declare a giant array knowing it will always be big enough).
 

The Assignment

You are allowed to work in pairs on this assignment if you desire.

In the class source folder eventually there will be a file called "cards" which will contain the input for this task.  Your final program will read this file in, sort it appropriately, and printout the sorted card pile.  You are to hand in your program (named appropriately for the drop box), and your output.  Your program must be commented suitably so I can understand the data structures you use, and your algorithms.  A description of why you made the choices you did would also be extremely helpful (necessary). Because this is a more substantial project than previous ones you have two school weeks to complete the program. However, at the end of the first week you are to hand in a preliminary version. This version should at the very least, read in a file of the appropriate type and put the cards into some type of list, as well as having the ability to display lists of cards appropriately.