DS2 ARRAY Flashcards

1
Q

OBJECT VS BUILT-IN TYPE

A

object:
-create with new obj()
-addressed via reference that is stored in local variable points to memory address where values are stored
-there are wrapper classes for types (Integer class has parseInt method etc)
type:
-create with type_name variable name= value
-variable points to contents ofmemory address
-can be casted or transformed to a different type (char can become int, double can become float and int etc)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ARRAYS

A

collection of same type items or objects
has index pointer to access position (indexing at 0)
throws ArrayOutOfBoundsException in java
public field length
size is predetermined cant extend or shorten
takes up memory serially

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ERATOSTHENIS SIEVE

A

give n>=2 get all primes <n
approach 1:
n length boolean array
start with all positions true
parse it starting from pos 2(assuming 0, 1 are not prime numbers)
every time i is prime find multiplicands<n and set to false
if we encounter false continue

java code:
primes(n){
~boolean[] a = new boolean[n];
~for (int i = 2; i < n; i++){a[i] = true;}
~for(int i = 2; i < n; i++)
~~{if(a[i])
~~~{for(int j = i; j * i < n; j++) //start at j=i cause for j<i previous iterations have already found it//
~~~~{a[i*j] = false;} }}}

time complexity is n/2 + n/3 +…+n/n = n(1/2+…+1/n)= n*lnn so same scale as O(nlogn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

TYPES OF ARRAY COPIES
(concerns arrays that contain objects types cannot have a copied reference so a semi-deep type copy is a deep copy)

A

-shallow copy: copy of reference
is basically 2 names for the same thing
points to same address
if one is altered both are altered
-semi-deep copy: copy of content reference
2 different arrays
contents point to the same address
changing one array doesnt affect the other one but changing something in the contents of one array does
(changing a field of obj a in ar1 changes the field of that object in ar2 but changing ar1[i] doesnt change ar2[i])
-deep copy: use new() when copying contents
only values are the same

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

SIZE CHANGE

A

arrays are static data structures so to change size we have to copy the contents to a different size array and delete the original
caution with memory leaks especially in c and c++ since memory isnt freed automatically when deleting a reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

MULTI-DIMENTIONAL ARRAYS

A

declare as obj[i][j]
store serially in lines(first line then second etc)
thats why we parse i then j optimally

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

APPLICATION IN GRAPH REPRESENTATION

A

v : vertices (dots)
e : edges (connectors)
using adjacency matrix
if i and j vertices are connected ar[i][j] and [j][i] set to 1 else set to 0
n^2 memory
O(1) time complexity for adjacency check

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

SPARCE ARRAYS

A

2-D array majority of contents have value 0
better store as 1-D array with coords and value
k( non zero points) memory instead of n^2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

TRIANGULAR ARRAYS

A

above or below diagonal is all 0
there are more effective ways of storing but depends on application and trade offs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

HONOURABLE MENTIONS

A

classes Vector and ArrayList are dynamic but slower to parse (get() method)
usually use array when creating data structures like trees

How well did you know this?
1
Not at all
2
3
4
5
Perfectly