0

My merge sorting code in c++ , this code working for 100,000 int number but its not work for 1,000,000 and 10,000,000 int number and exit. Where is my code wrong? I get the numbers randomly from the input. Are the random numbers I get correct?

void merge(int arr[], int p, int q, int r) {
  int n1 = q - p + 1;
  int n2 = r - q;

  int L[n1], M[n2];

  for (int i = 0; i < n1; i++)
    L[i] = arr[p + i];
  for (int j = 0; j < n2; j++)
    M[j] = arr[q + 1 + j];

  
  int i, j, k;
  i = 0;
  j = 0;
  k = p;

  
  while (i < n1 && j < n2) {
    if (L[i] <= M[j]) {
      arr[k] = L[i];
      i++;
    } else {
      arr[k] = M[j];
      j++;
    }
    k++;
  }

  
  while (i < n1) {
    arr[k] = L[i];
    i++;
    k++;
  }

  while (j < n2) {
    arr[k] = M[j];
    j++;
    k++;
  }
}


void mergeSort(int arr[], int l, int r) {
  if (l < r) {
    int m = l + (r - l) / 2;
    mergeSort(arr, l, m);
    mergeSort(arr, m + 1, r);
    merge(arr, l, m, r);
  }
}



void printArray(int arr[], int size) {
  for (int i = 0; i < size; i++)
    cout << arr[i] << " ";
  cout << endl;
}




int main()
{
    int n;
    cout << "Enter Number: ";
    cin >> n;
   int array[n];
   for(int i = 0; i<n; i++) {
      array[i]=rand()%1000;
   }


auto start2 = chrono::steady_clock::now();
  mergeSort(array, 0, n - 1);
 auto end2 = chrono::steady_clock::now();
auto diff2 = end2 - start2;
cout << "Time to sort for Merge: "<<chrono::duration <double, milli> (diff2).count() << " ms" << endl;

    return 0;
}

I try with DevC++ but exit from codes. i want to using this get random code for another sorting.Is it correct to get input in this way?

  • 3
    Your code is not really valid C++, because [C++ doesn't have variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). – Some programmer dude Nov 05 '22 at 07:35
  • 1
    And those variable-length arrays are likely the source of your problem. The compiler that have VLA's put them on the stack, like other local variables. And the stack is a very limited resource. On e.g. Linux it's 8 MiB, which means you can have at most two million four-byte `int` elements (in total), if you don't have anything else on the stack (like other local variables, function arguments, no function calls *at all*). – Some programmer dude Nov 05 '22 at 07:37
  • `int array[n];` is not legal C++. For legal C++ use a vector, that will also solve your problem. – john Nov 05 '22 at 07:38
  • To solve your problem, please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn C++ properly. – Some programmer dude Nov 05 '22 at 07:39
  • Where or what are you learning C++ from? It seems an outdated source still using "C" style arrays. And check this : https://en.cppreference.com/w/cpp/types/numeric_limits it might explain to you why an int is not good enough to hold large values. As for std::vector have a look here https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/ – Pepijn Kramer Nov 05 '22 at 08:34

0 Answers0