【问题标题】:My C++ code doesn't work well for larger, random arrays我的 C++ 代码不适用于更大的随机数组
【发布时间】:2014-11-29 21:19:22
【问题描述】:

我编写了代码来生成一个大小为 1,000,000 的数组,并使用合并排序和插入排序算法对其进行排序,然后测量每个进程所花费的时间。它适用于最大 100,000 的数组大小。但是一旦我将n(数组大小)更改为 1,000,000 并编译并运行它,程序就可以工作了。我正在使用 Code::Blocks 13 编译器,它具有遵循 c++11 ISO 语言标准的 g++ 编译器。

代码如下:

#include<iostream>
#include<climits>
#include<cstdlib>
#include<ctime>
#include<chrono>
using namespace std;
void insertion_sort(long inputSize, long  *inputArray);
void merge(long *inputArray, long low, long mid, long high);
void merge_sort(long *inputArray, long low, long high);
int main()
{
    srand(time(NULL));
    long n = 1000000;
    long inputArray1 [n];     //Declare the two arrays of size n
    long inputArray2 [n];
    cout << endl << "Unsorted List" << endl;
    for (long i = 0; i < n; i++) //initialize the arrays of size n with random n numbers
    {
        inputArray1[i] = inputArray2[i] = rand(); //Generate a random number
        //cout<<inputArray1[i]<<" ";  //Display each element in the array
    }
    std::chrono::high_resolution_clock::time_point Start = std::chrono::high_resolution_clock::now();
    insertion_sort(n, inputArray1); //calling the insertion_sort to sort the array of size n
    std::chrono::high_resolution_clock::time_point End =  std::chrono::high_resolution_clock::now();
    long long timeTaken = std::chrono::duration_cast<std::chrono::microseconds>(End - Start).count();
    cout << endl << endl << "Sorted list using Insertion Sort" << endl;
    /*for (int x=0;x<n;x++){     //Display the sorted array which was sorted using insertion_sort
          cout<<inputArray1[x]<<" "<<" ";
    }*/
    cout << endl << "Time taken = " << timeTaken << " microseconds";
    Start = std::chrono::high_resolution_clock::now();
    merge_sort(inputArray2, 0, n); //calling merge_sort to sort the array of size n
    End  = std::chrono::high_resolution_clock::now();
    timeTaken = std::chrono::duration_cast<std::chrono::microseconds>(End - Start).count();
    cout << endl << endl << "Sorted list using Merge Sort" << endl;
    /*for (int x=0;x<n;x++){     //Display the sorted array which was sorted using merge_sort
        cout<<inputArray2[x]<<" "<<" ";
     }*/
    cout << endl << "Time taken = " << timeTaken << " microseconds";
    return 0;
}

void insertion_sort(long inputSize, long *inputArray)
{
    for (long i = 1; i < inputSize; i++)
    {
        long key = inputArray[i];
        long b = i - 1;
        while ((b >= 0) && (inputArray[b] > key))
        {
            inputArray[b + 1] = inputArray[b];
            b = b - 1;
        }
        inputArray[b + 1] = key;

    }
    return;
}

void merge_sort(long *inputArray, long low, long high)
{
    if (low < high)
    {
        long mid = (low + high) / 2;
        merge_sort(inputArray, low, mid);
        merge_sort(inputArray, mid + 1, high);
        merge(inputArray, low, mid, high);
    }
    return;
}

void merge(long *inputArray, long low, long mid, long high)
{
    long n1 = mid - low + 1;
    long n2 = high - mid;
    long *L = new long [n1 + 1];
    long *R = new long [n2 + 1];
    for (long i = 0; i <= n1; i++)
    {
        L[i] = inputArray[low + i];

    }
    for (long j = 0; j <= n2; j++)
    {
        R[j] = inputArray[mid + j + 1];
    }
    L[n1] = INT_MAX;
    R[n2] = INT_MAX;
    long i = 0;
    long j = 0;
    for (long k = low; k <= high; k++)
    {
        if (L[i] <= R[j] )
        {
            inputArray[k] = L[i];
            i = i + 1;
        }
        else
        {
            inputArray[k] = R[j];
            j = j + 1;
        }

    }
    delete[] L;
    delete[] R;
    return;
}

【问题讨论】:

  • 看起来像堆栈溢出。使用std::vector 而不是原生数组。
  • 请附上代码。
  • “.exe 文件停止工作”究竟是什么意思?它崩溃了吗?永远不会完成?等
  • 是的。它崩溃了我附上了代码
  • 或者,如果您不想使用 std::vector,请使用 new 分配数组并在最后删除它们。

标签: c++


【解决方案1】:

您可以使用 new 在堆上创建数组:

long* inputArray1 = new long[n];    
long* inputArray2 = new long[n];

不要忘记在 main 的末尾删除它。

【讨论】:

  • @DushaniWellappili - 只是好奇,但是 100 万长的插入排序似乎需要相当长的时间,您为此花费了多少时间?
  • 是的。这需要很多时间。我得到了 1165.76 秒。我没有运行 1000 万的插入排序。因为它需要更多的时间。
【解决方案2】:
long n=1000000;
long inputArray1 [n];    
long inputArray2 [n];

首先,可变长度数组在标准 C++ 中无效,n 应该是 const

假设long为4字节,这两个数组占用8MB的栈大小,比普通栈的大小要大。程序因为stack overflow而崩溃。

要解决此问题,请使用 std::vector 而不是原生数组。

【讨论】:

  • 感谢您的回答。但是除了使用向量之外还有其他方法吗?因为我实际上并不熟悉它。
  • @DushaniWellappili 是的,让它成为静态的。
  • 可变长度数组在标准 C++ 中有效,因为 c++11 或 c++14 现在不确定。
  • @Quest 谢谢。是的,它在 c++11 中也有效
  • @DushaniWellappili 我还没有检查过 C++14,但肯定不是 C++11,一些编译器支持它作为扩展。
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 2015-12-10
  • 2016-01-01
相关资源
最近更新 更多