对于我的编程项目,我应该编写一个程序来整理磁盘上的整数(即脱机排序)。我首先应该生成一些随机整数,写入所有这些整数,然后读取其中两个整数进行交换,然后将它们写回到磁盘,然后重复这些步骤,直到数字排序。我能够生成随机数字就好了,打开文件没有问题,但是当试图写入文件时崩溃。下面是我用来实现排序,包含排序算法的程序代码的崩溃我的程序片段,而且代码的片段:C++外部冒泡排序
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。 有没有办法解决这个错误,并使这个排序程序的工作?试过我可能试过的一切,并且我看不到一行代码导致程序崩溃或导致其他问题。
实际上,根据我对Knuth的排序和搜索的回忆,我认为冒泡排序可以适用于排序未存储在内存中的大型数据集。 (当然,他是在谈论9磁道磁带而不是磁盘,但气泡排序的好处在于它可以顺序访问所有东西。 –
我还怀疑海报被指示使用气泡排序作为课程的一部分。我怀疑对整数1TB数据库进行排序的最佳方式是:将0.5 GB的块读入内存,使用quick_sort进行排序并写出,然后对生成的200个流进行合并排序。 –