Exercise 3: Sort Fields
- Due Apr 6, 2024 by 11:59pm
- Points 12
- Submitting a file upload
- Available Apr 2, 2024 at 12pm - Apr 8, 2024 at 11:59pm
In this exercise, you will sort based on multiple fields in item records stored in arrays. They must be sorted by, in priority order:
- Description (ascending lexical order)
- Rating (highest to lowest)
- Cost (lowest to highest)
You will only bother with the 2nd and 3rd fields if the first (or second) are equal. E.g., an item with a description of "cork" comes before "mop" (end of story). However, for 2 mops, you must then order by rating, if ratings are also identical, then by cost.
Start with the following program: CSI318-Exercise 3 (Sort Fields).zip Download CSI318-Exercise 3 (Sort Fields).zip and implement the multi-field sorting in 2 ways (both in the same program):
Quicksort
- Implement the Quicksort and Partition algorithms from the book as C++ functions.
- Partition uses ≤ to compare elements; thus, overload operator <= for the Item struct provided. The operator will have to test 1-3 fields in 2 Item objects.
- Call your quicksort function to sort the array of items at the appropriate place in main().
A Stable Sort
- Implement a stable sort in the program (quicksort is not a stable sort) as one or more C++ functions. Write your function so that a comparison function can be passed (use a pointer-to-function parameter).
- Write 3 different comparison functions to compare items separately by description, rating, cost.
- Call your sorting function 3 times. First, to sort the array of items by the least significant field, then the field of middle significance, then the most significant field. Pass the appropriate comparison function in each case.
Note that both sorting methods should result in the same, correct order based on the 3 fields. Generate sufficient random items to test thoroughly.
Be sure to document your code well. Provide URLs for any code researched online.
Zip up the folder containing the completed Visual Studio solution (remember to remove some files per the instruction video). Upload the zipped folder, a separate text file (program output), and image (plot) here for this assignment.
Rubric
Criteria | Ratings | Pts | |
---|---|---|---|
Implemented quicksort algorithm as function.
threshold:
pts
|
pts
--
|
||
Implemented partition algorithm as function.
threshold:
pts
|
pts
--
|
||
Implemented Item operator <=.
threshold:
pts
|
pts
--
|
||
Wrote comparison functions (description, rating, cost).
threshold:
pts
|
pts
--
|
||
Implement stable sort with comparison (ptr-to-function) parameter.
threshold:
pts
|
pts
--
|
||
Calls sorting functions: quicksort, 3 stable sorts (first cost, then
rating, then description).
threshold:
pts
|
pts
--
|
||
Documentation, design, conventions.
threshold:
pts
|
pts
--
|
||
Total Points:
12
out of 12
|