I need to make an array in which I have to put 10 000 000 000 small numbers. I came to the fact that this is not possible.

The code is working if the place is LONG everywhere to put INT. But int does not fit this number of numbers. So, I will explain what I am doing in this code. I have a two-dimensional array, I need to sort it, it seems to me that the easiest way to do this is through a regular array, and based on that I had a problem with "10,000,000,000."

Is it possible to do this as I had planned, that is, without really changing the code? If not then ask for advice.

Difficult answers are not welcome because I just started programming and have little understanding of various functions.

int n = sc.nextInt(); int m = sc.nextInt(); if (n>=100000 || m>=100000) System.exit(0); int [][] array = new int [n][m]; long f =n*m; int [] arraypomoc = new int [n*m]; long poc =0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { array[i][j]=sc.nextInt(); arraypomoc[poc]=array[i][j]; poc++; } } for (long i = 0; i < arraypomoc.length-1; i++) { for (long j = 0; j < arraypomoc.length-i-1; j++) { if(arraypomoc[j]>arraypomoc[j+1]){ int temp = arraypomoc[j]; arraypomoc[j]=arraypomoc[j+1]; arraypomoc[j+1]=temp; } } } 
  • You do not have enough memory. Under 10 billion values, you need at least 40 GB of memory - Serhii Dikobrazko

1 answer 1

Uncomplicated answer:

 int getArrayPomoc(int [][] array, int m, long index) { int i = index / m; int j = index % m; return array[i][j]; } void setArrayPomoc(int [][] array, int m, long index, int value) { int i = index / m; int j = index % m; array[i][j] = value; } void sort2dArrayAs1d(int [][] array, int n, int m) { long f = n * m; for (long i = 0; i < f - 1; i++) { for (long j = 0; j < f - i - 1; j++) { int vj = getArrayPomoc(array, m, j); int vj1 = getArrayPomoc(array, m, j + 1); if(vj > vj1){ setArrayPomoc(array, m, j, vj1); setArrayPomoc(array, m, j + 1, vj); } } } } int compareArrays(int [][] a, int [][] b, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] < b[i][j]) { return -1; } else if (a[i][j] < b[i][j]) { return 1; } } } return 0; } ... int [][] a = new int [n][m]; int [][] b = new int [n][m]; // initialize arrays a and b sort2dArrayAs1d(a, n, m); sort2dArrayAs1d(b, n, m); // compare arrays a and b int result = compareArrays(a, b, n, m); 
  • It seems to understand, everything is not difficult, I like it, but I do not understand one thing, how can I continue to use this sorted array? - Nataliia Vetsko pm
  • @NataliiaVetsko Wait, we know nothing about this. You wrote in the question that you need to sort the two-dimensional array as one-dimensional. But why - did not say. - Igor
  • I agree, my mistake. I just did not think that I could sort it out so that I could not use it later. And if I say that I need to do this for two arrays and then just compare if they are the same, will it change a lot? - Nataliia Vetsko
  • Sorry, maybe I'm stupid, but I still can't compare the two sorted arrays, if I do it through if then it gives an error that the void type is not allowed. - Nataliia Vetsko pm
  • @NataliiaVetsko added compareArrays - Igor compareArrays