C++ 调试合并排序 void CensusData::mergeSort(int类型){ 如果(类型==0) MERGE_SORT(type,0,data.size()); } void CensusData::MERGE_SORT(int-type、int-p、int-r){ //int-q; //请看斯坦福大学优秀课程编程摘要第15讲。它涵盖了各种类型,包括合并:

C++ 调试合并排序 void CensusData::mergeSort(int类型){ 如果(类型==0) MERGE_SORT(type,0,data.size()); } void CensusData::MERGE_SORT(int-type、int-p、int-r){ //int-q; //请看斯坦福大学优秀课程编程摘要第15讲。它涵盖了各种类型,包括合并:,c++,vector,mergesort,C++,Vector,Mergesort,您甚至可以从SourceForge获得源代码: 为什么需要手动排序算法?课堂作业(练习生成算法)该类是否要求您实现合并,或者您是否可以使用,或者甚至更好。使用其中任何一个,尤其是后者,都会使mergesort实现变得微不足道。无关:城市/州成员不应该是动态的。它们是std::string对象。因此它们本质上已经是动态的。并且您的记录构造函数参数应该是动态的常量参考 void CensusData::mergeSort(int type) { if(type == 0) M

您甚至可以从SourceForge获得源代码:


为什么需要手动排序算法?课堂作业(练习生成算法)该类是否要求您实现合并,或者您是否可以使用,或者甚至更好。使用其中任何一个,尤其是后者,都会使mergesort实现变得微不足道。无关:城市/州成员不应该是动态的。它们是
std::string
对象。因此它们本质上已经是动态的。并且您的记录构造函数参数应该是动态的<代码>常量参考
void CensusData::mergeSort(int type) {
    if(type == 0)
        MERGE_SORT(type, 0, data.size());
}

void CensusData::MERGE_SORT(int type, int p, int r){
    //int q;
    //cout << "data size " << data.size() << endl;
    std::cout << "MERGE_SORT START ///("<< p << ", " << r << ")" <<std::endl;
    if(p < r)
    {
        int q = (p + r)/2;
        MERGE_SORT(type, p, q);
        MERGE_SORT(type, q + 1, r);
        MERGE(type, p, q ,r);
    }
}

void CensusData::MERGE(int type, int p, int q, int r){
    if(type == 0)
    {
        std::cout << "MERGING" << std::endl;
        //int n1;
        //int n2;
        int n1 = q - p + 1;
        int n2 = r - q;
        int L[n1 + 1];
        int R[n2 + 1];
        for(int i = 1; i < n1; i++)
        {
            cout << "filling Left Array" << endl;
            L[i] = data[p + i - 1]->population;
        }
        for(int j = 1; j < n2; j++)
        {
            cout << "filling Right Array" << endl;
            R[j] = data[q + j]->population;
        }
        int i = 1;
        int j = 1;
        for(int k = p; p < r; p++)
        {
            cout << "for loop: " << endl;
            if(L[i] <= R[j])
            {
                cout << "TRUE" << endl;
                data[k]->population = L[j];
                i = i + 1;
            }
            /*else if(data[k]->population == R[j])
            {
                cout << "FALSE" << endl;
                j = j + 1;
            }*/
            else
            {
                data[k]->population = R[j];
                j = j + 1;
            }
        }

    }
}
   class Record {                         // declaration of a Record
   public:
      std::string* city;
      std::string* state;
      int population;
      Record(std::string&, std::string&, int);
      ~Record();
   };
   std::vector<Record*> data;