繁体   English   中英

使用Vector的Mergesort无法正确排序

[英]Mergesort using Vectors not sorting correctly

我正在使用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

我认为在代码段中存在问题,我们在循环中将tmpL和tmpR向量中的值推入到这里,将leftIndex和rightIndex都初始化为0,而您应该使用start和rightIndex初始化为mid + 1并运行leftIndex并运行循环直到中间和结尾。

暂无
暂无

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

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