0% found this document useful (0 votes)
4 views

Array in Data Structure.pptx

Arrays are collections of similar data items stored at contiguous memory locations, allowing for random access via index numbers. They are useful for sorting, searching, and efficiently storing multiple values of the same type, but have limitations such as static size and memory wastage. Sparse matrices, a specific type of two-dimensional array, optimize storage and computation by only storing non-zero elements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array in Data Structure.pptx

Arrays are collections of similar data items stored at contiguous memory locations, allowing for random access via index numbers. They are useful for sorting, searching, and efficiently storing multiple values of the same type, but have limitations such as static size and memory wastage. Sparse matrices, a specific type of two-dimensional array, optimize storage and computation by only storing non-zero elements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Array in Data Structure

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

► There are some of the properties of an array that are


listed as follows –
• Each element in an array is of the same data type and
carries the same size that is 4 bytes.
• Elements in the array are stored at contiguous memory
locations from which the first element is stored at the
smallest memory location.
• Elements of the array can be randomly accessed since
we can calculate the address of each element of the
array with the given base address and the size of the
data element.
Representation of an array

► We can represent an array in various ways in different programming


languages.

As per the above illustration, there are some of the


following important points -
•Index starts with 0.
•The array's length is 10, which means we can store 10
elements.
•Each element in the array can be accessed via its
index.
Why are arrays required?

► Arrays are useful because -


• Sorting and searching a value in an array is easier.
• Arrays are best to process multiple values quickly and easily.
• Arrays are good for storing multiple values in a single variable - In
computer programming, most cases require storing a large number of data
of a similar type. To store such an amount of data, we need to define a
large number of variables. It would be very difficult to remember the
names of all the variables while writing the programs. Instead of naming all
the variables with a different name, it is better to define an array and store
all the elements into it.
Memory allocation of an 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

• Traversal - This operation is used to print the elements of the array.


• Insertion - It is used to add an element at a particular index.
• Deletion - It is used to delete an element from a particular index.
• Search - It is used to search an element using the given index or by the
value.
• Update - It updates an element at a particular index.
Advantages of Array

• 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?

► There are the following benefits of using the sparse matrix -


► Storage - We know that a sparse matrix contains lesser non-zero
elements than zero, so less memory can be used to store elements. It
evaluates only the non-zero elements.
► Computing time: In the case of searching in sparse matrix, we need to
traverse only the non-zero elements rather than traversing all the sparse
matrix elements. It saves computing time by logically designing a data
structure traversing non-zero elements.
Representation of sparse matrix
► Array representation of the sparse matrix
► Representing a sparse matrix by a 2D array leads to the wastage of lots of
memory. This is because zeroes in the matrix are of no use, so storing
zeroes with non-zero elements is wastage of memory. To avoid such
wastage, we can store only non-zero elements. If we store only non-zero
elements, it reduces the traversal time and the storage space.
► In 2D array representation of sparse matrix, there are three fields used that
are named as -

•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 –

You might also like