ROAD MAP
Introduction toArray
Types of Array
1D- Array
Memory Representation of 1D Array
Operations on 1D Array
3.
ARRAY
An arrayis 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.
4.
WHY WE NEEDARRAY?
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).
ONE DIMENSION ARRAYOR 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] ;
7.
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
Length = 10
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
EXAMPLE
Suppose wewant 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
= 1004
12.
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
13.
TRAVERSING IN LINEARARRAY
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
14.
TRAVERSING ALGORITHM
1. [InitializeCounter] 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
15.
EXAMPLE
Suppose wehave an array of length 3 which stores
colour name in it.
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
K:=0
Red Yellow Blue
16.
EXAMPLE
Repeat step 3and 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
K:=1 + 1 = 2
17.
EXAMPLE
K=2
Repeat step 3and 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
18.
INSERTION IN LINEARARRAY
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
19.
INSERTION ALGORITHM
Algorithm:
1. SetJ:=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
K= Position in linear array where we insert item
20.
EXAMPLE
Suppose wetake 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]).
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.
Red Blue Pink Green White
21.
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
J : = 3
22.
EXAMPLE
While (3 >=2)
[MoveJth 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
23.
EXAMPLE
While (2 >=2)
[MoveJth 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
24.
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
25.
DELETION IN LINEARALGORITHM
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
26.
DELETION ALGORITHM
1. SetITEM:=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
27.
EXAMPLE
Suppose wetake 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]).
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
Red Blue Pink Green White Orange
28.
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]
29.
EXAMPLE
Repeat for J=2to 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]
30.
Repeat for J=4to 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
N = 5
0 1 2 3 4 5 6 7
Red Pink Green White Orange