首页 > 解决方案 > 将随机数生成到打印到 Txt 文件的数组中。在点击文本文件之前,需要对数字进行排序

问题描述

所以我的目标是基本上将 40,000 到 1,000,000 的随机数打印到 txt 文件中,并使用堆方法进行排序。我可以用随机数很好地打印到文本文件,但我有点坚持使用堆方法对它们进行排序。在看了一些教程后,我开始了一个方法,并在中途迷失了方向。有什么想法/有用的评论吗?我从字面上看是堆栈溢出的新手(明智地发布),所以如果我没有正确地将它放在这里,请原谅我。谢谢!

//Heapsort algorithm

#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <stdio.h> 
#include <stdlib.h> 

using namespace std;


/*class generateDataSet {

public: static const int MAX_VALUE = 1000000;
public: static const bool SORTED = false;
public: static const bool REVERSE_SORTED = false;

};
*/
int main()
{
    const int array= 16;
    int arrayNum[array];

    srand(time(NULL));


    int upB = 1000000;
    int loB = 40000;

    int temp;
    ofstream inputFile;
    inputFile.open("randomData.txt");


    if (inputFile.is_open())
    {
        for (int i = 0; i < array; i++)
        {



            arrayNum[i] = (rand()% (upB - loB + 1)) + loB;



            inputFile << arrayNum[i] << "\n";

        }

    }

    return 0;

}

void MaxHeapify(int d[], int i, int n)
{

    int j;
    int temp;

    temp = d[i];

    j = 2 * 1;

    while (j <= n)
    {
        if (j < n && d[j + 1] > d[j])
            j = j + 1;
        if (temp > d[j])
            break;
        else if (temp <= d[j])
        {
            d[j / 2] = d[j];
            j = 2 * j;
        }
    }
}


void heapSort(int d[], int n)
{

    int i;
    int temp;
    for (i = n; i >= 2; i);
}

标签: sortingvisual-c++heapsort

解决方案


推荐阅读