This question is about sorting with the array container in c++. I am doing some comparisons of sort code. I have a bubble sort and an array container in c++ in Mac Xcode. The task is to build an array of random numbers and then sort those numbers and see how long it takes for the sort to be completed. The bubble sort requires 59 seconds for 100,000 random numbers and 6,086 seconds for 1,000,000 random numbers. I have not asked the bubble sort to try more than a million numbers. The array container requires one second for 100,000 random numbers and fails at run time when presented with an array size of a million random numbers. The failure occurs just inside main{...}, before any other statements are processed. The first statement inside main, is a simple cout << "this is a test of sorts \n";
In Xcode, the error message is: Thread1: EXC_BAD_ACCESS(code=2, address=0xfff5ecbd258)
The applicable code statements are shown below.
Note that I retyped the code here, so a typo is possible. I hope the basic idea is complete enough for you to see what is going on.
#define ARRAY_SIZE 1000000
#include <iterator>
#include <array>
The following code is inside main
std::array<double, ARRAY_SIZE> A1 = {0};
// Here, I have some code that fills array A1 with random numbers using the rand() function
std::sort(A1.begin(), A1.end());
// some code that prints a selection of the A1 array.
My question is... is there an upper limit for the array container? if there is, how can I know what it is, and is there a work_around? The max_size statement simply returns the size handed to the array by the #define statement.
Is there a better, faster, way to sort a million items?
Thank you.