You are on page 1of 31

LECTURE # 04

INTRODUCTION TO ARRAY

1
ROAD MAP
 Introductionto Array
 Types of Array
 1D- Array
 Memory Representation of 1D Array
 Operations on 1D Array

2
ARRAY
 An array is a data structure, which can store a fixed-size
collection of elements of the same data type
(homogeneous).
 An array is used to represent a list of numbers , or a list
of names.
 For Example :

1. List of employees in an organization.


2. Test scores of a class of students.
3. List of students in the college.

3
WHY WE NEED ARRAY?
 Array is particularly useful when we are dealing with lot of
variables of the same type.

 For example, lets say I need to store the marks in math subject of
100 students. To solve this particular problem, either I have to
create the 100 variables of int type or create an array of int type
with the size 100.

 Obviously the second option is best, because keeping track of all


the 100 different variables is a tedious task.

 On the other hand, dealing with array is simple and easy, all 100
values can be stored in the same array at different indexes (0 to 99).
4
TYPES OF ARRAY
1. One-dimensional arrays

2. Two-dimensional arrays

3. Multidimensional arrays

5
ONE DIMENSION ARRAY OR 1D ARRAY
 A variable which represent the list of items using only
one index (subscript) is called one-dimensional array.
 1D array also called Linear array

 For Example , if we want to represent a set of five


numbers say(35,40,20,57,19), by an array variable
number, then number is declared as follows
int number [5] ;

6
LENGTH OF ARRAY
 N = length of array

Length = UB – LB + 1

 UB = Upper Bound or Largest Index


 LB= Lower Bound or smallest Index

For Example
UB = 9
LB=0
Length = UB – LB + 1
Length = 9 – 0 + 1
7
Length = 10
REPRESENTATION IN MEMORY (1D-
ARRAY)

8
REPRESENTATION IN MEMORY (1D-
ARRAY)
 Address of any element in Array =
LOC(LA[k])=Base (LA) + w (k - LB)

 LOC(LA[k]) =Address of element LA[k] of the Array


LA
 Base (LA) = Base Address of LA
 w = No. of words per memory cell for the Array LA
 k = Any element of Array

9
EXAMPLE

10
EXAMPLE
 Suppose we want to find out Loc (A [3]). For it, we
have:
Base (A) = 1000
w = 2 bytes (Because an integer takes two bytes in the
memory).
K=3
LB = 1

After putting these values in the given formula, we get:


LOC (A [3]) = 1000 + 2 (3 – 1)
= 1000 + 2 (2)
= 1000 + 4 11
= 1004
OPERATIONS PERFORMED BY ARRAY
 Traversal: Processing each element in the list
 Search : Find the location of the element with
given value or the record with a given key
 Insertion : Adding new element to the list

 Deletion : Removing an element from the list

 Sorting : Arranging the elements in some type of


order
 Merging : Combining two list into single list

12
TRAVERSING IN LINEAR ARRAY
 Traversing a Linear Array

TraverseArray (LA, LB, UB)


Function: This algorithm traverse LA
applying an operation PROCESS to each
element of LA
Input: LA is a Linear Array with Lower Bound
LB and Upper bound UB

13
TRAVERSING ALGORITHM
1. [Initialize Counter] Set K:=LB
2. Repeat Steps 3 and 4 while K≤UB
3. [Visit element] Apply PROCESS to
LA[K]
4. [Increase counter] Set K:=K+1
[End of Step 2 loop]
5. Exit

14
EXAMPLE
 Suppose we have an array of length 3 which stores colour
name in it.
Red Yellow Blue
arr[0] arr[1] arr[2]
Input : Traversing a linear array (LA) with Lower Bound (LB)
and Upper Bound (UB)
Output: Traverse Linear Array by applying operation to each
element

[Initialize Counter]
Set K:= LB
15
K:=0
EXAMPLE
Repeat step 3 and 4 while K <= UB (0 <= 2)
[Visit Element] Apply process to LA[1]
[Increment Counter]
Set K:= K + 1
K:=0 + 1 = 1
K=1

Repeat step 3 and 4 while K <= UB (1 <= 2)


[Visit Element] Apply process to LA[1]
[Increment Counter]
Set K:= K + 1 16
K:=1 + 1 = 2
EXAMPLE
K=2
Repeat step 3 and 4 while K <= UB (2 <= 2)
[Visit Element] Apply process to LA[1]
[Increment Counter]
Set K:= K + 1
K:=2 + 1 = 3
End of Loop
Exit

17
INSERTION IN LINEAR ARRAY
 InsertElement (LA, ITEM, N, K)
 Function: This algorithm insert an element in a
Linear Array at required position
 Input: LA is a Linear Array having N elements
 ITEM is the element to be inserted at given position
K
 Precondition: K≤N where K is a +ve integer

18
INSERTION ALGORITHM
Algorithm:
1. Set J:=N
2. Repeat step 3 and 4 while J>=K
3. [Move Jth element downward] Set
LA[J + 1] := LA[J]
4. Set J:= J – 1 [Decrease Counter]
5. Set LA[K]:= ITEM [Insert Element]
6. Set N:= N + 1 [Reset N]
7. Exit

N= Filled position of linear array


19
K= Position in linear array where we insert item
EXAMPLE
 Suppose we take an array of length 8 (LA[8]). Initial 5
positions are occupied with data and remaining 3 are
free. We want to insert an element on 3rd position of an
array (LA[2]).
Red Blue Pink Green White

0 1 2 3 4 5 6 7
Input : ( LA, N , K, ITEM), LA is linear array with N
elements and K is a positive integer such that
K<=N.
Output: Insert an element ITEM into the Kth position in
LA.
20
EXAMPLE
ITEM = Orange
J=N=4
K=2
While (4 >=2)
[Move Jth element downward]
Set LA[J + 1] : = LA[J]
LA[4 + 1] : = LA[4]
LA [5] : = LA [4]
[Decrease Counter]
Set J : = J – 1
4=4–1 21
J:=3
EXAMPLE
While (3 >=2)
[Move Jth element downward]
Set LA[J + 1] : = LA[J]
LA[3 + 1] : = LA[3]
LA [4] : = LA [3]
[Decrease Counter]
Set J : = J – 1
3=3–1
J:=2
22
EXAMPLE
While (2 >=2)
[Move Jth element downward]
Set LA[J + 1] : = LA[J]
LA[2 + 1] : = LA[2]
LA [3] : = LA [2]
[Decrease Counter]
Set J : = J – 1
2=2–1
J:=1
End of Loop 23
EXAMPLE
[Insert Element]
Set LA[K] : = ITEM
LA[2] : = Orange

[Reset N(5)]
Set N: = N + 1
4=4+1
N:=5
Exit
Red Blue Orange Pink Green White 24
DELETION IN LINEAR ALGORITHM
 DeleteElement (LA, ITEM, N, K)
 Function: This algorithm delete an element from a
given position in Linear Array
 Input: LA is a Linear Array having N elements
 K is the position given from which ITEM
needs to be deleted
 Output: ITEM is the element deleted from
the given position K
 Precondition: K≤N where K is a +ve integer

25
DELETION ALGORITHM
1. Set ITEM:=LA[K]
2. Repeat for J:=K to N-J
3. [Move Jth element upward] Set
LA[J] := LA[J+1]
[End of Step 2 loop]
4. [Reset N] N:= N-1
5. Exit

26
EXAMPLE
 Suppose we take an array of length 8 (LA[8]). Initial 6
positions are occupied with data and remaining 3 are
free. We want to delete an element on 2nd position of an
array (LA[1]).
Red Blue Pink Green White Orange

0 1 2 3 4 5 6 7
Input: (LA, N , K , ITEM) where LA is Linear Array with
N elements and K is a positive integer such that K <= N.
Output: Delete the Kth element from LA

27
EXAMPLE
Set ITEM : = LA[K]
ITEM : = Blue
Blue : = LA[1]
Delete element of LA[1]
Repeat for J = K to N-1
Repeat for J=1 to 5-1
[Move J + [1+1] element upward]
Set LA[1] : = LA[1 + 1]
LA[1] : = LA[2]
28
EXAMPLE
Repeat for J=2 to 4-1
[Move J + [2+1] element upward]
Set LA[2] : = LA[2 + 1]
LA[2] : = LA[3]

Repeat for J=3 to 3-1


[Move J + [3+1] element upward]
Set LA[3] : = LA[3 + 1]
LA[3] : = LA[4]
29
Repeat for J=4 to 2-1
[Move J + [4+1] element upward]
Set LA[4] : = LA[4 + 1]
LA[4] : = LA[5]

End of Loop
Reset the number N(6) of elements in LA
Set N : = N – 1
Red N = 5 Green White Orange
Pink
0 1 2 3 4 5 6 7
30
SUMMARY
 Introduction to array

 Introduction to 1D - Array

 Operations on 1D- Array

31

You might also like