You are on page 1of 20

CLO-1

Lecture#02-Part I
Linear Array and Basic Operations
Course: Data Structures and Algorithm (CE-205)
Course Teacher: Ms. Aneeta Siddiqui & Maheen Danish

Contact Info:
Room No: BS-03, CED
Email: aarshad@ssuet.edu.pk

1
Array Examples

2
Linear Array
• A linear array is a list of finite numbers n of homogenous data elements or a
Container of elements
• We can calculate the length, or a total number of elements, of a linear array
(LA), by the given formula:

Length(LA) = UB – LB +1 153
154
Where, -
-
UB: Upper bound or Largest index -
LB: Lower bound or Smallest index 234

For example:
• we have an array of employees, in which lower bound is 153 and
the upper bound is 234, so we can calculate the total number of
elements in the list as

234-153+1=82
3
Linear Array

4
Memory Representation of Linear Array

5
Memory Representation of Linear Array
• LOC(LA[K]): address of the element LA[K] of the array LA.
• Using Base (LA), the computer calculates the address of any element of LA
by the following formula:

LOC(LA[K]) = Base(LA) + w(K-Lower bound)

Where, w is the number of words per memory cell of the array LA.
• The time to calculate LOC(LA[K]) is the same for any value of K.
• Given any subscript K, one can locate and access the content of LA[K],
without scanning any other element if the LA.

6
7
Operations on Linear Array
We can perform various operations on a linear array:
• Traversing-  processing each element of the array list.
• Inserting- adding new elements in the array list.
• Deleting- removing an element from the array list.
• Sorting- arranging the elements of the list in some sorting
order.
• Merging- combining the elements of two array lists in a single
array list.

8
Traversing LA
• We can process each element of an array with the help of an index
set.
• Consider a Linear Array(LA) list given with lower bound(LB) and
upper bound(UB) that contains ‘K' number of elements.  We can
traverse the list with the given algorithm.

9
Insertion in LA

10
Insertion in LA

11
Insertion in LA
• Array insertion, of an element at the end of the list, is quite
simple.  We can do so by providing the unallocated space to the
new element. 
• On the other hand, if we need to insert an element in the
middle of an array, lots of internal shifting is required to insert
the new element.  i.e. half of the elements must be moved
downward, to the new location, to enter the new element.

12
Deletion in LA
• deletion of the element from the end of the array, is quite simple.  
• But, if we need to delete an element from the middle of the array,
then on average, half of the elements must be moved upward, in
order to fill the blank space, after deleting the element from the
specified location.

13
Array Operations

14
Two-Dimensional Array
•   two-dimensional m x n array A is a collection of m.n data elements such
A
that each element is specified by a pair of integers (such as J, K), called
subscripts, with the property that

15
Two-Dimensional Array

16
Two-Dimensional Array

17
Two-Dimensional Array

We can calculate the length, or a total number of elements, of a linear array


(LA), by the given formula:
Length(LA) = UB – LB +1

Same as linear array because we always assume that our arrays are regular,
such that the LB of any dimension of an array is equal to 1

18
Two-Dimensional Array in Memory

19
20

You might also like