I am practising java programming. I encountered a problem which requires 1000 x 1000 matrix which stores integers value less than 1500
I would want to navigate across all the elements
I might need to fetch max element and its 4 adjacent elements.
What is the best data structure which doesn't affect performance?
1 2 3 4
5 6 7 8
9 10 11 12
12 14 15 16
for the element 11 --> 7, 10, 12, 15 are adjacent elements.
what is wrong with the 2d array data structure?
to get an adjecent of a number at i,j return [i-1][j],[i+1][j],[i][j+1],i[j-1] (you will have to deal with cases where i is zero etc)...
as performance goes, its O(1), doesn't get any better than that...
If you are talking about finding the location of the element. if the matrix is sorted you can simply do a binary search.
A 2dim Array of shorts:
-> short [][] matrix = new short[1000][1000];
| Added variable matrix of type short[][] with initial value [[S#1794d431
Generate in a second, few MB in size. What could be better than that?
hi i have an array of 9 elements
10 , 5 , 2 , 4 , 6 , 1 , 3 , 2 , 6
i have to sort this using merge sort algorithm . My question is that 6 is been used twice so how will it affect the sorting.
To understand how this would be sorted lets have a look at how the merge sort algorithm really works.
split each element into partitions of size 1
recursively merge adjancent partitions
for i = leftPartStartIndex to rightPartLastIndex inclusive
if leftPartHeadValue <= rightPartHeadValue
copy leftPartHeadValue
else: copy rightPartHeadValue
copy elements back to original array
So lets take the given array [10 , 5 , 2 , 4 , 6 , 1 , 3 , 2 , 6] Here's how the algorithm really runs
Initially split the array into partitions of size 1. So each value in the array is individual partition.
Now merge 10 in position 0 and 5 in position 1 together into 1 partition.
Since 10 > 5, Copy the 5 into a new temporary array of size 2, now the right partition is empty and hence copy 10 into it, so you now have a temporary array which is [5,10] Then copy it back into the original array in the positions making it
[5,10,2,4,6,1,3,2,6]
Now merge the partitions [5,10] to [2] in the 2nd index. Since 5 > 2, copy 2 to the temporary array. Since the right partition is empty copy 5 to the temporary array to the right and similar with 10 thus making [2,5,10]. Now copy the items back to the original array.
[2,5,10,4,6,1,3,2,6]
Now merge the partitions [4,6] and since 4 < 6, copy 4 to the left partition and 6 to the right partition of a temporary array and copy them back to the original array. In this case, the array is unchanged and still looks the same.
[2,5,10,4,6,1,3,2,6].
Now merge partitions [2,5,10] to [4,6], Since 2 < 4, copy 2 to the temporary array, then since 4 < 5, copy 4 to the array, then since 5 < 6 copy 5 and so on until we have a new partition temporary array [2,4,5,6,10] and then copy the elements to the original array [2,4,5,6,10,1,3,2,6]
Now merge the items [1,3] and similarly with [2,6] since value on left side of the partition is lower than that on the right. Then merge [1,3] to [2,6]. Since 1 < 2, copy 1 to temporary array and then 2 and so on to get the new partition [1,2,3,6] and copy it back to the original array making it [2,4,5,6,10,1,2,3,6].
Finally we have 2 partitions to merge i.e. [2,4,5,6,10] and [1,2,3,6]. Repeat the similar process, since 1 < 2, copy 1 to the temporary array and then 2 (because 2 in left partition <= 2 in right partition) so the value 2 from the left partition gets copied to the temporary array. Then 4 is compared to 2 and the value 2 from the right partition is copied to the new temporary array. Then 4 > 3, so 3 is copied and then the 4 < 6 makes 4 be copied and then 5 < 6 (right partition) makes 5 be copied to the temporary array. When we encounter 6 (left partition) to 6 (right partition) and following the condition 6 <= 6 the value 6 from the left partition gets copied to the temporary array. Then the 6 in the right partition is compared to 10 and 6 < 10 so, 6 gets copied to temporary array and then 10. Then the entire temporary array is copied to the original array thus making us obtain the final array. [1,2,2,3,4,5,6,6,10]
I have a a row of int numbers, e.g. 1 2 3 7 8 9. They are sorted.
I often need to insert numbers in this row, so that the row stays sorted, e.g. 4 => 1 2 3 4 7 8 9. Sometimes I have to read the row from the start to a number, that depends on the numbers in the row.
Which data type to choose best and how to insert the new number best?
If your sequence does not have repetitions you can use a SortedSet<Integer>, say, a TreeSet<Integer>, so that every time you and an element the sequence will remain sorted.
If the sequence does have repetitions check out Guava's sorted multiset.
try ArrayList. it is much easier to manipulate than a simple array. if you need to only work with primitives, this can still be done with an array of ints.
ArrayList<Integer> foo = new ArrayList<integer>;
foo.add(2,4) //puts a 4 at index 2, shifting everything else down
This is a conceptual question following on from a question (here) I recently got an answer to.
I will use a java example to demonstrate my question but I think this should hold for other similar languages (I could not say for sure -hopefully someone can confirm this or say otherwise). Basically I am wondering if there is any subtle difference between the possible uses for following (given how they are initialised):
double[] array1D = new double[max]
and
double[][] array2D = new double [max][1]
Assuming that we are initialising the second array to have only one column, is there really any need for the 1D array to exist? Does it have any quality that a 2D array doesn't have, or is it just there to confuse poor souls like myself? Furthermore I would like to know if array2D with only one column can do anything that array1Dcannot?
The main difference between array1D and array2D is that in the latter the rows (or columns, however you want to look at it) are individual objects that can be used independently of the entire array.
For example you can pass an entire row to a function, and the function is allowed to modify the row. To achieve the same with a 1D array you'd have to pass the original array, the start index of the row in the array, and length of the row.
Every reference to an object costs 4 bytes, so the memory usage increases with 4 bytes for each element, so it is a waste of memory to use a 2D array for something that a 1D array can do.
new double[max] creates an array of doubles (initialized with 0.0) with lengh = max
new double [max][1] creates an array of pointers each of them points to a double array with one element = 0.0
int A[2][3]={{1,2,3},{4,5,6}};
A 2D array is stored in the memory as follows. Entries in
row 0 are stored first followed by row 1 and so on.
2-D arrays are represented as a
contiguous block of n blocks each with size m (i.e. can
hold m integers(or any data type) in each block). The
entries are stored in the memory as shown above.Here n represent the number of rows and m represents the
number of columns.
In Java:
As with all arrays, the new keyword must be used to allocate memory for an array. For example,
int[][] a = new int[2][4];
This two-dimensional array will have two rows and four columns.
This actually allocates 3 objects: a one-dimensional array of 2 elements to hold each of the actual row arrays, and two one-dimensional arrays of 4 elements to represent the contents of the rows.
+-----+ +-----+-----+-----+-----+
|a[0] | -> | [0] | [1] | [2] | [3] |
| | +-----+-----+-----+-----+ In Java two-dimensional arrays are implemented is a
one-dimensional array of one-dimensional arrays -- like this.
+-----+
| | +-----+-----+-----+-----+
|a[1] | -> | [0] | [1] | [2] | [3] |
+-----+ +-----+-----+-----+-----+
In your case:
double[][] array2D = new double [max][1]
This actually allocates max+1 objects: a one-dimensional array of max elements to hold each of the actual row arrays, and max one-dimensional arrays of 1 elements to represent the contents of the rows.
Let me know if I am wroung .
Both will just do the same thing. Moreover you don't need to create a 2-dimensional array when all you need is just one row. A 2-dimensional array is used when you need to work with multiple 1-dimensional arrays.
Also, I think you need to initialize your 2-dimensional in this way to achieve it to work for 1-d array
double[][] array2D = new double[1][max]
Imagine this is 1D array
1 2 3 4 5 6 7 8 9
and this is 2D array
1
2
3
4
5
6
7
8
9
1 2 3 4 5 6 7 8 9
basically, 2D array can store more data than 1D array, and more complex than 1D array
You seem to have the misconception that it is normal for arrays to have two dimensions. This is not true.
In programming languages one-dimensional arrays are the norm, while two- and higher dimensional ones are rather rare (I can't remember when I last used one in Java).
Using a two-dimensional array instead of a one-dimensional one would be possible, but it would obscure the meaning of the array and make accessing the array elements more complicated.
Let's say I have a 2D array:
int[][] a = new int[4][3];
populated such that:
1 2 3
4 5 6
7 8 9
2 5 7
Is there any shortcut method in java to extract lets say column 1 as single array:
array1 = {1 4 7 2};
Currently what I am doing is traversing through the whole 2D matrix and with if condition (if j==0), I traverse over the rows and add values to 1D array.
Just wondering if there is any standard method offered in java for such tasks.
No there is no shortcut to doing this. You have to loop over the arrays, switching the x & y indices.
There is no such build-in method. You have to write a simple loop.
You might want to consider use of a matrix library. But this is pretty simple stuff - if this is all you need, you can probably write it quicker than you can get up to speed on a library.