Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Array in Data Structure

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Array in Data Structure

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Array in Data Structure

Array
• Arrays are defined as the collection of similar types of
data items stored at contiguous memory locations.
• Each item in an array is indexed starting with 0 .
• Each element in an array is accessed through its index.
• It is one of the simplest data structures where each data
element can be randomly accessed by using its index
number.
Properties of array
• 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

•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?
• 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
Memory allocation of an array

0 (zero-based indexing): The first element of the array will be


arr[0].
1 (one-based indexing): The first element of the array will be
arr[1].
n (n - based indexing): The first element of the array can
reside at any random index number.
How to access an element from the
array?
• We required the information given below to access any
random element from the array –
• Base Address of the array.
• Size of an element in bytes.
• Type of indexing, array follows.
• The formula to calculate the address to access an array
element –
Example
Suppose an array, A[-10 ..... +2 ] having Base address
(BA) = 999 and size of an element = 2 bytes, find the
location of A[-1].
Solution:
L(A[-1]) = 999 + 2 x [(-1) - (-10)]
= 999 + 18
= 1017
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.
Insertion at the Beginning of an
Array
• Algorithm
Insertion at the Given Index of an Array
//Algorithm
We assume A is an array with N elements. The maximum numbers of elements it can store is defined by
MAX.
Insertion After the Given Index of an
Array
Insertion Before the Given Index of
an Array
Array - Deletion Operation
Algorithm
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Array - Search Operation
• Algorithm:
• 1. Start
• 2. Set J = 0
• 3. Repeat steps 4 and 5 while J < N
• 4. IF LA[J] is equal ITEM THEN GOTO STEP 6
• 5. Set J = J +1
• 6. PRINT J, ITEM
• 7. Stop
Array - Traversal Operation
Algorithm
1 Start
2. Initialize an Array of certain size and
datatype.
3. Initialize another variable ‘i’ with 0.
4. Print the ith value in the array and
increment i.
5. Repeat Step 4 until the end of the array is
reached.
6. End
Array - Update Operation
• 1. Start
• 2. Set LA[K-1] = ITEM
• 3. Stop
Array - Display Operation
Algorithms
1. Start
2. Print all the elements in the Array
3. Stop
Complexity of Array operations
Time Complexity:
Operation Average Case Worst Case
Access O(1) O(1)
Search O(n) O(n)
Insertion O(n) O(n)
Deletion O(n) O(n)

Space Complexity:

In array, space complexity for worst case is O(n).


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.

You might also like