c++ - 使用 Vectors 的 Mergesort 排序不正确

标签 c++ pointers vector mergesort

我正在研究 Mergesort 以刷新我对它的内存。我是根据我创建的人的文本文件来做的。出于某种原因,我似乎无法调试......排序根本没有真正排序任何东西。我有正确的函数和循环,但肯定有一些我没有注意到的小事。我将不胜感激任何帮助。谢谢!

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

struct Person {
    string DOB;
    string balance;
    string firstName;
    string lastName;
    string state;

    Person() { }

    Person(string DOB, string firstName, string lastName, string state, string balance) {
        this->DOB = DOB;
        this->firstName = firstName;
        this->lastName = lastName;
        this->state = state;
        this->balance = balance;
    }

    void print() {
        cout << DOB << " "
        << balance << " "
        << firstName<< " "
        << lastName << " "
        << state  << " "
        << balance  << "\n";
    }
};

void print(vector<Person*> arr, int size) { // print function for array debuggin'
    for (int i = 0; i < size - 1; i++) {
        cout << arr[i]->lastName << " | ";
    }
    cout << endl;
}

void merge(vector<Person*> arr, int start, int mid, int end) {
    int leftSize = mid - start + 1;
    int rightSize = end - mid;
    int leftIndex, rightIndex;
    int masterIndex = start;

    vector<Person*> tmpR; // arrays to represent our two sections of the total array
    vector<Person*> tmpL;

    for (leftIndex = 0; leftIndex < leftSize; leftIndex++) { // copying our values from our master array into our subarrays
        tmpL.push_back(arr[leftIndex]);
    }
    for(rightIndex = 0; rightIndex < rightSize; rightIndex++) {
        tmpR.push_back(arr[rightIndex]);
    }

    //print(tmpL, leftSize); // print those sub arrays out for debugging
    //print(tmpR, rightSize);

    leftIndex = 0;
    rightIndex = 0;

    while (leftIndex < leftSize && rightIndex < rightSize) { // compares L and R subarrays and picks the last name first in the alphabet to go first
        if (tmpL[leftIndex]->lastName < tmpR[rightIndex]->lastName) {
            arr[masterIndex] = tmpL[leftIndex];
            leftIndex++;
        } else {
            arr[masterIndex] = tmpR[rightIndex];
            rightIndex++;
        }
        masterIndex += 1;
    }

    while (leftIndex < leftSize) { // the two following while conditions empty the remaining ordered last names from the subArray that is not empty
        arr[masterIndex] = tmpL[leftIndex];
        leftIndex++;
        masterIndex++;
    }
    while (rightIndex < rightSize) {
        arr[masterIndex] = tmpR[rightIndex];
        rightIndex++;
        masterIndex++;
    }

}

void split(vector<Person*> arr, int start, int end) {
    if (start < end) {
        int mid = (start+end) / 2;
        split(arr, start, mid);
        split(arr, mid + 1, end);
        merge(arr, start, mid, end);
    }
}

void readIn() {
    string DOB;
    string ssNumber;
    string bankBalance;
    string firstName;
    string lastName;
    string state;
    int size;
    vector<Person*> pVector;

    ifstream fin;
    fin.open("data1.txt");
    if (fin.fail()) {
        cout << ("error reading file");
        exit(1);
    }

    while (!fin.eof()) { // pulls in our data and stores it in a poiinter to a person object
        fin >> DOB >> ssNumber >> firstName >> lastName >> state >> bankBalance;
        Person *tmp = new Person(DOB, firstName, lastName, state, bankBalance);
        pVector.push_back(tmp);
    }
    size = (int) pVector.size() - 1;

    fin.close(); // closes the input stream previously opened.
    cout << size << endl;
    sleep(2);

    split(pVector, 0, size-1);

    for (int i = 0; i < size; i++) {
        cout << pVector[i]->lastName << endl;
    }
}

int main() {
    readIn();
    return 0;
}

最佳答案

据我所知,您正在按值传递 vector,即它被复制并且在调用站点上您保留原始的。

尝试通过引用传递 vector 。

void split(vector<Person*>&    arr, int start, int end) {
                         ^^^

void merge(vector<Person*>& arr, int start, int mid, int end) {
                         ^^^

void print(vector<Person*> const& arr, int size) {
                          ^^^^^^^  // You don't want to modify it here

关于c++ - 使用 Vectors 的 Mergesort 排序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34120778/

相关文章:

c - 这些是 C 中兼容的函数类型吗?

c - 矩阵幂和指针

c++ - 删除[]后剩余的堆数组的剩余元素

C++ vector emplace_back 调用复制构造函数

c++ - 直接列表初始化编译成功,但是正常直接初始化失败,为什么?

C++ : Make multiple constructors with the same argument types

c++ - 多尺度检测循环在 OpenCV 的 HOG 检测中是如何工作的?

c# - pinvoke c 函数 - System.BadImageFormatException

c++ - 指针差异

c++ - 如何创建一个类来接受包含任何类型数据的 vector ?