C++ Help Sorting multiple categories written in string. I have to create a baseb
ID: 674696 • Letter: C
Question
C++ Help Sorting multiple categories written in string.
I have to create a baseball program that will make reports
My question is, how do I go about effiecently coding the sort to where it will find the league and then sort the names alphabetically within that league? Our porgram has the capabilty to get too large to use a bubble sort. i have done some research on this and was leaning towards using the #include <algorithm> to sort my arrays/structs.....Most everything I find online uses vectors and vectors, #include <algorithm> aren't in our book. Our instructer encourages us to find solutions online so, how should I go about this?
Below is a list of the program requirements
The program will allow the user to enter league. ie; major, minor, little league....whatever name of league they want to enter. They do want it to be able to add leagues whenever needed, seeing how they change from year to year
Within the league, they will need to have the names of the players in that league have their names sorted by last name first alphabetically. Their will be only two names to error check for, no three part names with spaces
Each player will have stats associated with their name including offensive ranking 1-10, defensive ranking 1-0, 40 times, and position they played.
Of course a function will be made passing all the information after user cin's the information for each category. Then the program will need to sort all the information by league, and then by name. All the other data will be displayed after the second sort.
Thank-you!!!
Explanation / Answer
Bubble sort is not an effective sorting technique to sort the array.
You can use quick sort to sort the array and it can reduce the running time for your algorithm. Algorithm is not required here and simply arrays can be used to store the values of the players.
For sorting you can use quick sort.
working c++ code for quick sort
#include <iostream>
#include <algorithm>
void print(int *a, int n)
{
int i = 0;
while(i < n){
cout << a[i] << ",";
i++;
}
cout << " ";
}
int partition(int *arr, int left_side, int right_side) {
int mid = left_side + (right_side - left_side) / 2;
int pivot = arr[mid];
swap(arr[mid],arr[left_side]);
int i = left_side + 1;
int j = right_side;
while (i <= j) {
while(i <= j && arr[i] <= pivot) {
i++;
}
while(i <= j && arr[j] > pivot) {
j--;
}
if (i < j) {
std::swap(arr[i], arr[j]);
}
}
swap(arr[i - 1],arr[left_side]);
return i - 1;
}
void quicksort(int *arr, int left_side, int right_side, int sz){
if (left_side >= right_side) {
return;
}
int part = partition(arr, left_side, right_side);
quicksort(arr, left_side, part - 1, sz);
quicksort(arr, part + 1, right_side, sz);
}
int main() {
int arr[8] = {110, 5, 10,3 ,22, 100, 1, 23};
int sz = sizeof(arr)/sizeof(arr[0]);
print(arr, sz);
quicksort(arr, 0, sz - 1, sz);
print(arr, sz);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.