In: Computer Science
Make quicksort functions to sort this class, first by ID then by athleteEvaluation
athleteID: an unique identifier for the athlete
athletePosition: an abbreviation of the expected player’s position on the team. We will use an enumerated data type to store the position within our BUAthlete class. A definition of the enumerated data type is provided below. Don't forget that enumerated data types are actually stored as integers.
enum Position {OL, QB, RB, WR, TE, DL, DE, LB, CB, S, K};
CLASS:
class BUAthlete : public NCAAAthlete{
protected:
int athleteID2;
string athletePosition;
int athleteEvaluation;
public:
void setID2(int id);
void setathletePosition(string pos);
void setEval(int eval);
int getID2();
string getathletePosition();
int getEval();
string toString();
};
HELLO
I HAVE DESIGNED 2 FUNCTIONS OF QUICK SORT TO SORT BY AS PER YOUR REQUIREMENT
PLEASE GO THROUGH IT
void swap(BUAthlete* a, BUAthlete* b)
{
BUAthlete t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partitionByID (BUAthlete arr[], int low, int high)
{
int pivot = arr[high].getID2(); // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j].getID2() <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSortById(BUAthlete arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partitionByID(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSortById(arr, low, pi - 1);
quickSortById(arr, pi + 1, high);
}
}
int partitionByEval(BUAthlete arr[], int low, int high)
{
int pivot = arr[high].getEval(); // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j].getEval() <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSortByEval(BUAthlete arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partitionByEval(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSortByEval(arr, low, pi - 1);
quickSortByEval(arr, pi + 1, high);
}
}