Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Data Structure and Algorithm

School of Computer Science and Engineering

LOVELY PROFESSIONAL UNIVERSITY


ARRAYS
 Linear arrays: Memory representation
 Traversal

 Insertion

 Deletion

2
INTRODUCTION
 Data Structure can be classified as:
 linear
 non-linear
 Linear (elements arranged in sequential in memory location) i.e.
array & linear link-list
 Non-linear such as a tree and graph.
 Operations:
 Traversing, Searching, Inserting, Deleting, Sorting, Merging
 Arrayis used to store a fix size for data and a link-list the data can
be varies in size.

3
INTRODUCTION

 Advantages of an Array:
Very simple
Economy – if full use of memory
Random accessed at the same time

 Disadvantage of an Array:
wasting memory if not fully used
4
LINEAR ARRAY
 Homogeneous data:
a) Elements are represented through indexes.
b) Elements are saved in sequential in memory locations.
 Number of elements, N –> length or size of an array.
If:
UB : upper bound ( the largest index)
LB : lower bound (the smallest index)
Then: N = UB – LB + 1
Length = N = UB when LB = 1 5
Travesing linear array

 Accessing each element of array only once so that it can be processed.


 Algorithm: Traversing Linear Array

 Let LA is a linear Array with lower bound LB and Upper bound UB. This Algorithm will apply
PROCESS to each element of LA.
Here LA is
LA 12 42 34 25
 0 1 2 3

LB UB

LOVELY PROFESSIONAL UNIVERSITY


Travesing linear array

 LA
 12 42 34 67 70
25
0 1 2 3 4 5
LB UB
 STEPS
 (1) START

 (2) Initialize counter set K=LB

 (3) Repeat Step 4th and 5th while(K<=UB)

 (4) Visit Element

 Apply PROCESS to LA[K]

 (5) Increment Counter

 K=K+1
 (6) STOP

LOVELY PROFESSIONAL UNIVERSITY


Insertion algorithm

• Inserting a new element into Array


• INSERT (LA,N,ITEAM,K)
• Let LA is linear Array of size N, this algorithm will insert ITEM at the Kth position of
LA, where K<N
• // K is integer positive where K<N and LB=0
• // Insert an element, ITEM in index K

LOVELY PROFESSIONAL UNIVERSITY


Insertion algorithm

Step:
1. START
N=5
2. Initialize Counter (Assign Counter)
Set J=N-1 25 30
LA
3. Repeat 4th and 5th while (J >= K)
4. Shift (Move) to the right all element from J 12 42 34
0 1 2 360 4 5

LA[J+1]=LA[J] LB
LB ITEM=70, K=2
5. Decrement Counter
J=J-1
6. Insert ITEM
LA[K]=ITEM
7. Reset N
N = N+1
8. STOP

LOVELY PROFESSIONAL UNIVERSITY


Deletion algorithm

Deleting element from Array


DELETE (LA,N,ITEAM,K)
Let LA is linear Array of size N, this algorithm will delete ITEM from Kth position
Where K is any positive integer such that K<N

LOVELY PROFESSIONAL UNIVERSITY


Deletion algorithm

Step:
1. START N=3
2. Set ITEM = LA[K] 12
LA 42 25 45 67 56
3. Initialize counter
set J=K; 0 1 2 3 4
4. Repeat 5th and 6th step while J<=N-2
LB
5. Move J+1th element shift to left LB
LA[J]=LA[J+1]
6. Increment Counter
J=J+1
7. Reset N=N-1

LOVELY PROFESSIONAL UNIVERSITY


LOVELY PROFESSIONAL UNIVERSITY

You might also like