CS1325 F16 HW6 1 PDF
CS1325 F16 HW6 1 PDF
CS1325 F16 HW6 1 PDF
Homework #6-1
Assigned Nov 10, due Nov 17 at 11:59 PM
This homework assignment gives you the opportunity to practice pointers, arrays, sorting algorithms,
and structures.
Point is initialized as illustrated in Fig. 2. We then sort the elements of Point so we view the
elements of studentArray in decreasing order of GPA. That is, after sorting, Point[0]->GPA >=
Point[1]->GPA >= Point[2]->GPA, etc., while the elements of studentArray are
untouched. The result is shown in Fig. 3.
1. Sorting procedure
There are many common sorting algorithms that could be used to sort these data. Most of these are
comparison based sorting algorithms, since they make decisions by comparing each array element
against others. Most often, comparison based sorting algorithms work by interchanging, or swapping,
elements within the array. One common and simple comparison based sorting algorithm is called
Bubble Sort. It works by making multiple passes through the data set and on each pass comparing two
adjacent array elements and swapping them if the right hand element is greater than the left hand
element. In this way, the largest element remaining is bubbled to the top of the array on each pass.
After all the passes are completed, the array is in sorted order. Here is the pseudo-code for one version
of the Bubble Sort. This is an algorithm that is based on two nested loops.
Outer loop index i runs from (n-1) down to 1
Inner loop index j runs from 0 to (i-1)
Compare myArr[j] and myArr[j+1]
if myArr[j] < myArr[j+1], swap the contents of myArr[j] and myArr[j+1]
Here, n refers to the number of elements in the array to be sorted. As given, this algorithm would sort
the original array myArr very well. However, it does modify the array itself, which is what we want to
avoid. In this assignment, we have to adapt the bubble sort to sort the Point array instead of the
original studentArray array. Although the sorting should be done according to the GPA values the
Point array is pointing to, the only values actually changed are the pointers in the Point array.
2. Additional requirements Make sure you meet all the requirements to avoid losing
points
1. Follow the requirements in the Homework Notes.
2. You will need to implement the following. Point and studentArray are arrays of Student *
and Student respectively.
a) A sorting function.
This function should implement a modified version of the above Bubble Sort. There are two arrays
involved, studentArray and Point, instead of the single array myArr, like in the pseudocode. The
pseudo code needs to be adapted as follows. In the comparison step, we compare Point[i]->GPA
with Point[j+1]->GPA, but in the swap step, we swap the contents of Point[j] and
Point[j+1]. The function modifies the Point array, but leaves studentArray unchanged.
void modifiedSort(Student *[], size_t); // Arguments are the array of
pointers and the size of the array
call the display function that displays the data in sorted order.
print (Now displaying data in the original order)
call the display function that displays the data in the original order (to verify the original data has not
been modified)