简体   繁体   中英

C++ Sorting Array struct by using QuickSort

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

These parts guarantees that no sorting will be done:

if (start < end) { 
//... do the sorting
} // else don't
//                   start  end                      
quickSort<YEAR>(Mas, 100000, 0);

In your partition() function, you could add some debug prints to get you on the right track:

    int pivotValue, pivotIndex, mid;

    mid = (start + end) / 2;
    std::cout << "size: " << vArray.size() << " start: " << start << " mid: " << mid
              << " end: " << end << "\n";
    // ...

    for (int scan = start + 1; scan <= end; scan++) {
        std::cout << "scan: " << scan << "\n";
        // ...

What you will notice is that you are actually indexing out of bounds:

size: 1 start: 0 mid: 0 end: 1
scan: 1

vArray[1] is illegal when vArray.size() is 1 so the program has undefined behaviour.

Your Info has a char Name[10] member which means that only names up to 9 characters long can be stored in it. Your names.txt file contains 59 names that are longer. You will therefor write out of bounds, probably into the following SurName member.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM