尝试将伪代码转换为MERGE SORT的实际代码

时间:2013-10-11 08:02:34

标签: c++ mergesort

我正在尝试为家庭作业实现合并排序,我们应该这样做,因此它与这个伪代码非常相似(假设数组以1开头)。

/*MERGESORT(A, p, r)]
    if p < r
        q = (p+r)/2
        MERGESORT(A,p,q)
        MERGESORT(A,q + 1, r)
        MERGE(A,p,q,r)

MERGE(A,p,q,r)
    n1 = q - p + 1
    n2 = r - q
    let L[1...n1 + 1] and R[1...n2 + 1] be new arrays
    for i = 1 to n1
        L[i] = A[p + i - 1]
    for j = 1 to n2
        R[j] = A[q + j]
    L[n1 + 1] = INFINITY
    R[n2 + 1] = INFINITY
    i = 1
    j = 1
    for k = p to r
        if L[i] <= R[j]
            A[k] = L[i]
            i = i + 1
        else
            A[k] = R[j]
            j = j + 1*/

其中A是整个数组,p q和r是索引,A [p ... r]是需要排序的。 这就是我到目前为止所做的:

    void CensusData::mergeSort(int type) {
        if(type == 0) //STOPPED FOR DEBUGGING
            MERGE_SORT(type, 0, data.size() - 1);
    }

    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 WITH: (" << p << ", "<< q <<", " << r<< ")"<< std::endl;
            //int n1;
            //int n2;
            int n1 = q - p + 1;
            int n2 = r - q;
            cout << "N1: " << n1 <<" N2:" << n2 << endl;
            Record* L[n1 + 1];
            Record* R[n2 + 1];
            L[n1 + 1] = NULL;
            R[n2 + 1] = NULL;
            for(int i = 0; i < n1; i++)
            {
                if (L[i] == NULL)
                    continue;
                cout << "P, I: " << p <<", "<< i<< endl;
                cout << "filling array L: " << data[p + i]->population << endl;
                L[i] = data[p + i];
                cout<< L[i]->population << endl;
            }
            //cout << "J: " << j << endl;
            for(int j = 0; j < n2; j++)
            {
                if(R[j] == NULL)
                    continue;
                cout << "filling array R: " << data[q + j + 1]->population<<endl;
                R[j] = data[q + j + 1];
                cout << R[j]->population << endl;
            }

            int i = 0;
            int j = 0;
            for(int k = p; k <= r; k++)
            {
                if(L[i]->population < R[j]->population)
                {
                    cout << "TRUE" << endl;
                    data[k] = L[i];
                    i = i + 1;
                }
                else
                {
                    cout << "FALSE" << endl;
                    data[k] = R[j];
                    j = j + 1;
                }
            }
               std::vector<Record*>::iterator it = data.begin();
   while (it != data.end()) {
      std::cout << *(*it)->city << ", "
                << *(*it)->state << ", "
                << (*it)->population << std::endl;
        }
    }

我可以告诉你的是,它不起作用并且它在整个地方出现故障,我已经工作了6个小时并且什么也没做,帮助将不胜感激。注意:记录是一个也称为数据的向量。这里也是输入和输出:

Vina, California, 237
San Francisco, California, 812826
Santa Fe, New Mexico, 68642
Roseville, California, 1293
New York, New York, 283822
Potatoville, Brentland , 283822

输出:

Vina, California, 237
Santa Fe, New Mexico, 68642

Program received signal SIGSEGV, Segmentation fault.
0x00445dd7 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
    at /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/iostream:77
77        static ios_base::Init __ioinit;

3 个答案:

答案 0 :(得分:0)

这是我看到的一个问题:

    Record* L[n1 + 1];
    Record* R[n2 + 1];
    L[n1 + 1] = NULL;
    R[n2 + 1] = NULL;

您定义了一个x元素数组,然后尝试为x + 1元素设置一个值。 请记住,数组的索引编号为0到n-1。

答案 1 :(得分:0)

除了作业之外,您可能对RosettaCode's Sorting algorithms/Merge sort/C++感兴趣。

答案 2 :(得分:0)

除了你的索引越界问题外,还有什么意思

if (L[i] == NULL) 
    continue; 

在初始化?

如果数组是用NULL进行初始化的,那么你就不会跳过每一个元素,这看起来有点无意义,如果至少有一个元素是NULL,这可能偶然发生,你很有可能当你说

时,在下面的循环中崩溃
L[i]->population

顺便说一下,最后的循环永远不会终止。