Array in Data Structure.pptx
Array in Data Structure.pptx
UNIT 1
ANUSHREE PANSARE
Definition
► Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations. It is one of the simplest data structures
where each data element can be randomly accessed by using its index
number.
Properties of array
► As stated above, all the data elements of an array are stored at contiguous
locations in the main memory. The name of the array represents the base
address or the address of the first element in the main memory. Each
element of the array is represented by proper indexing.
► We can define the indexing of an array in the below ways -
1. 0 (zero-based indexing): The first element of the array will be arr[0.
2. 1 (one-based indexing): The first element of the array will be arr[1.
3. n (n - based indexing): The first element of the array can reside at any
random index number.
►
► In the above image, we have shown the memory allocation of an array arr
of size 5. The array follows a 0-based indexing approach. The base
address of the array is 100 bytes. It is the address of arr[0. Here, the size
of the data type used is 4 bytes; therefore, each element will take 4 bytes
in the memory.
Basic operations
• Array provides the single name for the group of variables of the same
type. Therefore, it is easy to remember the name of all the elements of an
array.
• Traversing an array is a very simple process; we just need to increment
the base address of the array in order to visit each element one by one.
• Any element in the array can be directly accessed by using the index.
Disadvantages of Array
• Array is homogenous. It means that the elements with similar data type
can be stored in it.
• In array, there is static memory allocation that is size of an array cannot be
altered.
• There will be wastage of memory if we store less number of elements than
the declared size.
1 DIMENTIONAL ARRAY
► A one dimensional array is the simplest form of an array in which the element
are stored linearly and can be accessed individually by specifying index
2D ARRAY
Sparse Matrix
► What is a matrix?
► A matrix can be defined as a two-dimensional array
having 'm' rows and 'n' columns. A matrix with m rows
and n columns is called m × n matrix. It is a set of
numbers that are arranged in the horizontal or vertical
lines of entries.
► For example –
What is a sparse matrix?
► Sparse matrices are those matrices that have the majority of their
elements equal to zero. In other words, the sparse matrix can be defined
as the matrix that has a greater number of zero elements than the
non-zero elements.
Why is a sparse matrix required if we can use
the simple matrix to store elements?
•Row - It is the index of a row where a non-zero element is located in the matrix.
•Column - It is the index of the column where a non-zero element is located in
the matrix.
•Value - It is the value of the non-zero element that is located at the index (row,
column).
► Example -
► Let's understand the array representation of sparse matrix with the help of
the example given below -
► Consider the sparse matrix –