简体   繁体   English

C++ 使用 QuickSort 对数组结构进行排序

[英]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.我必须使用 QuickSort 算法对结构数组进行排序(不是内置的 function,我必须手动编写)并且还测量工作时间并与标准 C++ 排序 ZC1C425268E68384F1AB14ZA 进行比较。 After i compile my code the output file(sort.txt) looks the same.在我编译我的代码后,output 文件(sort.txt)看起来是一样的。 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:在主 function 中:

        quickSort<YEAR>(Mas, 0, pow(rows, i));

    Print(Mas);

My full code: https://www.codepile.net/pile/e2z3l7E0我的完整代码: https://www.codepile.net/pile/e2z3l7E0

Names.txt and Surnames.txt to generate: https://dropmefiles.com/riOmD要生成的 Names.txt 和 Surnames.txt: 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:在您的partition() function 中,您可以添加一些调试打印以使您走上正轨:

    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.vArray.size()1时, vArray[1]是非法的,因此程序具有未定义的行为。

Your Info has a char Name[10] member which means that only names up to 9 characters long can be stored in it.您的Info有一个char Name[10]成员,这意味着其中最多只能存储 9 个字符的名称。 Your names.txt file contains 59 names that are longer.您的names.txt文件包含 59 个较长的名称。 You will therefor write out of bounds, probably into the following SurName member.因此,您将越界写入,可能写入以下SurName成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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