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

问题描述 投票:-2回答:1

在这个程序中,我加载不同的数据集来测试Merge,Bubble和Insertion排序算法。我一直在研究零碎,这是一个早期的迭代。出于某种原因,我的代码随机崩溃,最后10次运行更多。在运行调试器后,它会让我在堆上随机点,但是没有提供有关程序问题的更多信息,我已经将它指向我的sort.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();

    }

sort.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");

    }

sort.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
c++ qt-creator
1个回答
0
投票

函数sort :: load()可能是问题:在调用push_back()时连续分配矢量数据集。使用足够大的数据集,您的程序将获得内存分配失败。

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

© www.soinside.com 2019 - 2024. All rights reserved.