简体   繁体   中英

C++ External bubble sort

For my programming project, I am supposed to write a program that sorts integers on a disk (ie offline sort). I am first supposed to generate some random integers, write all of them, and read two of those integers for swapping, and write them back to the disk, and repeat those steps until the numbers are sorted. I am able to generate random numbers just fine, and had no problem opening the file, but it crashes when an attempt is made to write to the file. Here is the fragment of the code for the program that I used to implement the sort, that contains the sorting algorithm, and the segment of the code that crashes my program:

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;
}

It crashes on the line of code finout.write(reinterpret_cast (rand_ints), sizeof(int) * 1); within the first for loop (line 52), with the following error: Exception thrown at 0x55916D16 (msvcp140d.dll) in ExternalSort.exe: 0xC0000005: Access violation reading location 0x00000001. Is there a way to fix this error and make this sorting program work? Tried everything I could have possibly tried, and I can't see a line of my code to cause the program to crash or cause other problems.

Since rand_ints is of type int , you probably mean reinterpret_cast<char *>(&rand_ints) (note the & ). Otherwise, you'll be making a pointer out of an integral value.

OTOH, trying to read two adjacent integers into the address of a single integer variable is very likely to cause problems.

Looking more deeply into your sorting algorithm, it seems to me that you attempted to generalize it for data elements of any size, and not just int s. However, it is still clearly array-oriented; if you wanted to deal with files, you probably have to pass the function either a filename or a fstream reference.

Also, unless you're required to use Bubble Sort, I'd strongly advise you against it, especially for on-disk sorting, unless you make sure your data set is very, very small (say, no more than a hundred numbers). For in-place sorting, I'd advise you to use Quick Sort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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