I have to sort the array of struct by using a QuickSort algorithm(not built-in function, i have to write it manually) and also take the measure of working time and compare with standard C++ sort function. After i compile my code the output file(sort.txt) looks the same. What have i done wrong?
struct Info
{
int Birth;
char Name[20];
char SurName[25];
};
template <keys T>
struct Comparer {
bool operator ()(const Info &m1, const Info &m2) const {
return (T == YEAR ? m1.Birth > m2.Birth : strcmp(m1.Name, m2.Name) > 0);
}
};
int partition(vector<Info> &vArray, int start, int end) {
int pivotValue, pivotIndex, mid;
mid = (start + end) / 2;
swap(vArray[start].Birth, vArray[mid].Birth);
swap(vArray[start].Name, vArray[mid].Name);
swap(vArray[start].SurName, vArray[mid].SurName);
pivotIndex = start;
pivotValue = vArray[start].Birth;
for (int scan = start + 1; scan <= end; scan++) {
if (vArray[scan].Birth < pivotValue) {
pivotIndex++;
swap(vArray[pivotIndex].Birth, vArray[scan].Birth);
swap(vArray[pivotIndex].Name, vArray[scan].Name);
swap(vArray[pivotIndex].SurName, vArray[scan].SurName);
}
}
swap(vArray[start].Birth, vArray[pivotIndex].Birth);
swap(vArray[start].Name, vArray[pivotIndex].Name);
swap(vArray[start].SurName, vArray[pivotIndex].SurName);
return pivotIndex;
}
template <keys T>
void quickSort(vector<Info>&vArray, int start, int end) {
int pivotPoint;
if (start < end) {
pivotPoint = partition(vArray, start, end);
quickSort<T>(vArray, start, pivotPoint - 1);
quickSort<T>(vArray, pivotPoint + 1, end);
}
}
void Print(const vector<Info> Mas)
{
string path = "sort.txt";
ofstream Out;
Out.open(path);
if (!Out.is_open()) {
cout << "wdw" << endl;
}
else {
for (int i = 0; i < (int)Mas.size(); i++)
Out << Mas[i].Name << " " << Mas[i].SurName << " " << Mas[i].Birth << "\n";
}
Out.close();
}
template<keys T>
bool isSorted(vector<Info> Mas)
{
Comparer<T> c;
for (int i = 0; i < Mas.size() - 1; i++)
if (c(Mas[i], Mas[i + 1]))
return false;
return true;
}
In main function:
quickSort<YEAR>(Mas, 0, pow(rows, i));
Print(Mas);
My full code: https://www.codepile.net/pile/e2z3l7E0
Names.txt and Surnames.txt to generate: https://dropmefiles.com/riOmD