【问题标题】:C++ External bubble sortC++ 外部冒泡排序
【发布时间】:2015-12-15 05:15:34
【问题描述】:

对于我的编程项目,我应该编写一个对磁盘上的整数进行排序的程序(即离线排序)。我首先应该生成一些随机整数,将它们全部写入,然后读取其中两个整数进行交换,然后将它们写回磁盘,然后重复这些步骤,直到对数字进行排序。我能够很好地生成随机数,并且打开文件没有问题,但是当尝试写入文件时它会崩溃。这是我用来实现排序的程序的代码片段,其中包含排序算法,以及使我的程序崩溃的代码片段:

void ReadAndWrite(int & rand_ints, int & pos_ints)
{
    int rand_ints2 = 0;

    GetNumber(pos_ints);

    srand(time(0));

    fstream finout;
    finout.open(SORTFILE, ios::binary | ios::in | ios::out);
    if (finout.is_open())
    {
        for (int i = 0; i < pos_ints; ++i)
        {
            rand_ints = rand() % 5;
            finout.write(reinterpret_cast <char *>(rand_ints), sizeof(int) * 1);
        }

        for (int i = 0; i < pos_ints; ++i)
        {
            finout.seekg(ios::beg);

            finout.read(reinterpret_cast <char *>(rand_ints), sizeof(int) * 2);

            bubbleSort(&rand_ints, 2, sizeof(int), compare_ints);

            finout.seekp(ios::app);

            finout.write(reinterpret_cast <char *>(rand_ints), sizeof(int) * 2);
        }

        finout.close();
    }
    else
    {
        cout << "File not opened!" << endl;
    }
}

void GetNumber(int & pos_ints)
{
    cout << "Enter a positive number: ";
    cin >> pos_ints;
}

void bubbleSort(void * base, size_t num, size_t width, int(*compar) (const void *, const void *))
{
    bool done = false;//Allows us to enter loop first time
    int hi = num - 1;//largest index is 1 less than the size of the array
    while (!done)
    {
        done = true;//assume the list is sorted
        for (int i = 0; i<hi; i++)
        {
            //pass thru the array up to 'hi'
            //if (arr[i+1]<arr[i])
            if (compar((void *)(((char *)base) + width*(i + 1)), (void *)(((char *)base) + width*(i))) < 0)
            {
                //if any pair are out of order
                done = false;//the list is not sorted

                             //int temp = arr[i];//swap them
                void * tempp = (void *) new char[width];
                memcpy_s(tempp, width, (((char *)base) + width*(i)), width);

                //arr[i] = arr[i+1];
                memcpy_s((((char *)base) + width*(i)), width, ((char *)base) + width*(i + 1), width);

                //arr[i+1]=temp;
                memcpy_s(((char *)base) + width*(i + 1), width, tempp, width);


                delete[] tempp;
            }
        }
        hi--;//at the end of a pass, largest item in that pass is in proper place; no need to go this far next time
    }
}

int compare_ints(const void * arg1, const void * arg2)
{
    int return_value = 0;

    if (*(int *)arg1 < *(int *)arg2)
        return_value = -1;

    else if (*(int *)arg1 > *(int *)arg2)
        return_value = 1;

    return return_value;
}

它在代码 finout.write(reinterpret_cast (rand_ints), sizeof(int) * 1);在第一个 for 循环(第 52 行)中,出现以下错误:ExternalSort.exe 中的 0x55916D16 (msvcp140d.dll) 引发异常:0xC0000005:访问冲突读取位置 0x00000001。 有没有办法修复这个错误并使这个排序程序工作?尝试了我可能尝试的所有方法,但我看不到导致程序崩溃或导致其他问题的代码行。

【问题讨论】:

    标签: c++ sorting


    【解决方案1】:

    由于rand_intsint 类型,您的意思可能是reinterpret_cast&lt;char *&gt;(&amp;rand_ints)(注意&amp;)。否则,您将使用整数值创建指针。

    OTOH,试图将两个相邻的整数读入单个整数变量的地址很可能会导致问题。

    更深入地研究您的排序算法,在我看来,您试图将它推广到任何大小的数据元素,而不仅仅是ints。但是,它仍然明显是面向数组的;如果您想处理文件,您可能必须向函数传递文件名或 fstream 引用。

    另外,除非您需要使用冒泡排序,否则我强烈建议您不要使用它,尤其是对于磁盘排序,除非您确保您的数据集非常非常小(例如,不超过一百个数字)。对于就地排序,我建议您使用快速排序。

    【讨论】:

    • 其实,根据我对 Knuth 的 Sorting and Searching 的回忆,我认为冒泡排序可以适用于对未存储在内存中的大型数据集进行排序。 (当然,他说的是 9 磁道磁带而不是磁盘,但冒泡排序的优点在于它按顺序访问所有内容。
    • 我还怀疑发帖人已被指示在他的课程中使用冒泡排序。我怀疑对 1TB 整数数据库进行排序的最佳方法是:将 .5gbyte 块读取到内存中,使用 quick_sort 进行排序并写出,然后对生成的 200 个流运行合并排序。
    猜你喜欢
    • 2014-03-26
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2017-06-21
    • 2012-07-19
    • 2015-09-06
    • 2013-03-09
    相关资源
    最近更新 更多