我的排序算法程序中的堆错误

Error on Heap in my Sorting Algorithms Program

本文关键字:错误 程序 排序 算法 我的      更新时间:2023-10-16

在这个程序中,我加载了不同的数据集来测试合并、冒泡和插入排序算法。我一直在做零碎的工作,这是一个早期的迭代。由于某种原因,我的代码随机崩溃,尤其是最后 10 次运行。运行调试器后,它将我带到堆上的随机位置,但没有提供有关程序问题的更多信息,我已经将其精确定位到我的排序.cpp文件中,但仍然无法获取它。我不相信这是内存泄漏,而且我没有使用指针,所以一切都应该存在。如果您能帮助我对程序进行故障排除,我将不胜感激。

主要

#include <iostream>
#include "merge.h"
#include "bubble.h"
#include "insertion.h"
#include "createfile.h"
#include "sort.h"
using namespace std;
int main()
{
cout << "You can do it Xavier, I believe in you" << endl;
//========================================
//Sort Random Sets
//========================================
SortRandom();
//========================================
//Sort Backward Sets
//========================================
// SortBackwards();
//========================================
//Sort sets with 20% Unique
//========================================
//    Sort20Percent();
//========================================
//Sort sets with 30% randomized
//========================================
//   Sort30Percent();
}

排序.cpp

#include "sort.h"
#include "fstream"
#include "iostream"
#include "bubble.h"
#include "insertion.h"
#include "merge.h"
using namespace std;
void SortRandom()
{
sort random;
//Sorts 10 set
random.load("Random(10).txt");
random.execute();
random.stats();
random.save("Random(10)Stats.txt");
cout << "====================" << endl;
//Sorts 1000 set
random.load("Random(1000).txt");
random.execute();
random.stats();
random.save("Random(1000)Stats.txt");
cout << "====================" << endl;
//Sorts 10,000 set
random.load("Random(10000).txt");
random.execute();
random.stats();
random.save("Random(10000)Stats.txt");
cout << "====================" << endl;
//Sorts 100,000 set
random.load("Random(100000).txt");
random.execute();
random.stats();
random.save("Random(100000)Stats.txt");
}

排序.h

#ifndef SORT_H
#define SORT_H
#include "algorithm.h"
#include <fstream>
#include <sstream>
#include <chrono>
using namespace std;
class sort : public algorithm
{
private:
vector<int> dataset;
string file;
string time1;
string time2;
string time3;
public:
//loads file into program
void load(string filename)
{
ifstream inFile;
ofstream writefile;
inFile.open(filename);
file = filename;
int entry;
string str;
while(std::getline(inFile, str))
{
inFile >> entry;
dataset.push_back(entry);
}
}
//Print the unsorted vector
void print()
{
for(int i=0;i<dataset.size();i++)
{
cout << dataset[i] << endl;
}
}
//Sort set and time it
void execute()
{
vector<int> temp1 = dataset;
vector<int> temp2 = dataset;
vector<int> temp3 = dataset;
//Time for bubblesort
using timer = std::chrono::high_resolution_clock;
timer::time_point start_time = timer::now();
bubblesort(temp1);
timer::time_point end_time = timer::now();
cout << "Total Time for BubbleSort: " << chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() << "ms" << endl;
//Time for MergeSort
using timer = std::chrono::high_resolution_clock;
timer::time_point start_time2 = timer::now();
MergeSort(temp2,0,temp2.size());
timer::time_point end_time2 = timer::now();
cout << "Total Time for MergeSort " << chrono::duration_cast<std::chrono::milliseconds>(end_time2 - start_time2).count() << "ms" << endl;
//Time for Insertion Sort
using timer = std::chrono::high_resolution_clock;
timer::time_point start_time3 = timer::now();
insertionsort(temp3,temp3.size());
timer::time_point end_time3 = timer::now();
cout << "Total Time for Insertion Sort " << chrono::duration_cast<std::chrono::milliseconds>(end_time3 - start_time3).count() << "ms" << endl;
//Save time variables
ostringstream x,y,z;
x << chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
time1 = x.str();
y << chrono::duration_cast<std::chrono::milliseconds>(end_time2 - start_time2).count();
time2 = y.str();
z << chrono::duration_cast<std::chrono::milliseconds>(end_time3 - start_time3).count();
time3 = z.str();
dataset = temp1;
}
//print sorted vector to screen
void display()
{
insertionsort(dataset,dataset.size());
for(int i=0;i<dataset.size();i++)
{
cout << dataset[i] << ',';
}
cout << endl;
cout << "===============================================" << endl;
}
//Print the size of the dataset,method and time it took
void stats()
{
cout << "To sort this data set of size " << dataset.size() << " using the bubblesort method took " << time1 << " ms." << endl;
cout << "To sort this data set of size " << dataset.size() << " using the mergesort method took " << time2 << " ms." << endl;
cout << "To sort this data set of size " << dataset.size() << " using the insert sort method took " << time3 << " ms." << endl;
}
//Save stats to a file
void save(string filename)
{
ofstream writefile;
writefile.open(filename);
writefile << "n";
writefile << "===========================================================================" << "n" << endl;
writefile << "To sort this data set of size " << dataset.size() << " using the bubblesort method took " << time1 << " ms." << endl;
writefile << "To sort this data set of size " << dataset.size() << " using the mergesort method took " << time2 << " ms." << endl;
writefile << "To sort this data set of size " << dataset.size() << " using the insert sort method took " << time3 << " ms." << endl;
writefile << "===========================================================================" << "n" << endl;
for(int i=0;i<dataset.size();i++)
{
writefile << dataset[i];
writefile << ',';
}
dataset.clear();
}

};
void SortRandom();
void SortBackwards();
void Sort20Percent();
void Sort30Percent();
#endif // SORT_H

函数 sort::load(( 可能是问题所在:当调用 push_back(( 时,向量数据集会连续重新分配。使用足够大的数据集,程序将出现内存分配失败。

解决此问题的一种方法是使用 dataset.reserve( MAX_DATASET_SIZE( 在程序开始时初始化其容量。