School of Computing in the College of Engineering at the University of Nebraska-Lincoln.
Before attending this lab:
-
Read and familiarize yourself with this handout.
-
Read Chapters 12 and 25 of the Computer Science I textbook
-
Watch Videos 13.1 thru 13.7 of the Computer Science I video series
For students in the online section: you may complete the lab on your own if you wish or you may team up with a partner of your choosing. You may consult with a lab instructor to get teamed up online (via Zoom).
For students in the face-to-face section: your lab instructor will team you up with a partner.
To encourage collaboration and a team environment, labs are be structured in a peer programming setup. At the start of each lab, you will be randomly paired up with another student (conflicts such as absences will be dealt with by the lab instructor). One of you will be designated the driver and the other the navigator.
The navigator will be responsible for reading the instructions and telling the driver what to do next. The driver will be in charge of the keyboard and workstation. Both driver and navigator are responsible for suggesting fixes and solutions together. Neither the navigator nor the driver is "in charge." Beyond your immediate pairing, you are encouraged to help and interact and with other pairs in the lab.
Each week you should alternate: if you were a driver last week, be a navigator next, etc. Resolve any issues (you were both drivers last week) within your pair. Ask the lab instructor to resolve issues only when you cannot come to a consensus.
Because of the peer programming setup of labs, it is absolutely essential that you complete any pre-lab activities and familiarize yourself with the handouts prior to coming to lab. Failure to do so will negatively impact your ability to collaborate and work with others which may mean that you will not be able to complete the lab.
At the end of this lab you should be familiar with the following
-
Understand basic searching and sorting algorithms
-
Understand how comparator functions work and their purpose
-
How to leverage standard search and sort algorithms built into a framework
Recursion has been utilized in several searching and sorting algorithms. In particular, quicksort and binary search both can be implemented using recursive functions. The quicksort algorithm works by choosing a pivot element and dividing a list into two sub-lists consisting of elements smaller than the pivot and elements that are larger than the pivot (ties can be broken either way). A recursive quicksort function can then be recursively called on each sub-list until a base case is reached: when the list to be sorted is of size 1 or 0 which, by definition, is already sorted.
Binary search can also be implemented in a recursive manner. Given a sorted array and a key to be searched for in the array, we can examine the middle element in the array. If the key is less than the middle element, we can recursively call the binary search function on the sub-list of all elements to the left of the middle element. If the key is greater than the middle element, we recursively call the binary search function on the sub-list of all elements to the right of the middle element.
Searching and sorting are two solved problems. That is, most languages and frameworks offer built-in functionality or standard implementations in their standard libraries to facilitate searching and sorting through collections. These implementations have been optimized, debugged and tested beyond anything that a single person could ever hope to accomplish. Thus, there is rarely ever a good justification for implementing your own searching and sorting methods. Instead, it is far more important to understand how to leverage the framework and utilize the functionality that it already provides.
Clone the project code for this lab from GitHub using the following URL: https://github.com/cbourke/CSCE155-C-Lab13
In this activity, you will implement a selection sort algorithm to sort an array of structures. Refer to lecture notes, your text, or any online source on how to implement the selection sort algorithm. The order by which you will sort the array will be according to the total team payroll (in dollars).
-
Familiarize yourself with the
Team
structure and the functions provided to you (the program automatically loads the data from the CSV file and provides an array of teams). -
Implement the
selectionSortTeamsByPayroll
function in theteam.c
file as specified by the header (team.h
) documentation. -
Compile and run your program (use make which produces an executable called
mlbDriver
) and verify the results.
The Team
structure has many different data fields; wins, losses (and win
percentage), team name, city, state, payroll, average salary. Suppose that
we wanted to re-sort teams according to one of these fields in either
ascending or descending order (or even some combination of fields?
state/city for example). Doing it the wrong way as above we would need
to implement no less than 16 different sort functions!
Most of the code would be the same though. Only the criteria on which we update the index of the minimum element would differ. Instead, it would be better to implement one sorting function and make it configurable: provide a means by which the function can determine the relative ordering of any two elements. The way of doing this in C is to define a comparator function.
Comparator functions must have the following signature:
int cmp(const void *a, const void *b);
Such a function takes two arguments a
and b
(specified by generic
void pointers) and returns an integer with the following "contract":
it returns
-
A negative value if
a < b
-
Zero if
a
is equal tob
-
A positive value if
a > b
Several examples have been provided for comparing structures based on several different criteria. The usual pattern is to cast the arguments to the expected types, then to examine the relevant field(s) and return a result that orders the two teams appropriately.
-
Implement the
selectionSortTeams
function by using the code in Activity 1 with appropriate modifications (use the providedcompar
function to find the minimum element each time) -
Look at the comparator functions provided to you and to the bubble sort algorithm example on how you might use a comparator function
-
Implement your own comparator function that orders
Team
s according to the total payroll in decreasing order. -
Use your comparator in the program to call your function.
The better way of doing this is to leverage the standard C library’s sorting function. The signature of this function is as follows:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
where
-
base
is the array to be sorted -
nmemb
is the number of elements in the array -
size
is the size of each element (usesizeof()
) -
compar
is a comparator function pointer
-
Examine the source files and observe how comparator functions are implemented and how the
qsort
function is called. -
Implement your own comparator function that orders
Team
s according to payroll in increasing order. -
Use your function in the
main
program to re-sort the array and print out the results.
The standard C library offers several search functions. The two that we will focus on are as follows.
-
lfind
a linear search implementation that does not require the array to be sorted. The function works by iterating through the array and calling your comparator function on the provided key. It returns the first instance such that the comparator returns zero (equal). -
bsearch
a binary search implementation that requires the array to be sorted according to the same comparator used to search.
Both functions require a comparator function (as used in sorting) and a key upon which to search. A key is a "dummy" instance of the same type as the array that contains values of fields that you're searching for.
-
Examine the searching code segment in the
mlbDriver.c
file and understand how each function is called. -
Observe that each of the 4 searches have different results. Discuss with your partner (or yourself) why this is the case.
-
Based on your observations add code to search the array for the team representing the Chicago Cubs.
-
Create a dummy
Team
key for the Cubs by using theinitTeam
function using empty strings and zero values except for the team name (which should be"Cubs"
). -
Sort the array by team name by calling
qsort
using the appropriate comparator function. -
Call the
bsearch
function using your key and the appropriate comparator function to find theTeam
representing the Chicago Cubs. -
Print out the team by using the
printTeam
function.
-
-
Examine your output and compare it to the
data/expectedOutput.txt
file; fix any errors before you submit. It may be useful to use an online "diff" tool (ex: https://www.diffchecker.com/) to highlight where your output may differ. -
Discuss with your partner: why were all the search results for a California (CA) team different?
-
Hand in your completed files:
team.h
team.c
mlbDriver.c
-
Even if you worked with a partner, you both should turn in all files.
Selection sort is a quadratic sorting algorithm, thus doubling the input
size (