Professional Documents
Culture Documents
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 :
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.
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
6
LENGTH OF ARRAY
N = length of array
Length = UB – LB + 1
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)
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
12
TRAVERSING IN LINEAR ARRAY
Traversing a Linear Array
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
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
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]
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
31