You are on page 1of 36

Created by:

Mehak
UID: 23837
SEEE-CSE
Introduction

• 2 WAYS OF REPRESENTING LINEAR STRUCTURES IN MEMORY:


1. Relationship between elements is represented by sequential
memory locations. Such linear data structures are called ARRAYS.
2. Relationship between elements is represented by pointers or links.
They are called LINKED LISTS.

• Operations performed on linear data structures,i.e. arrays are:


Linear Array(1-D Array)
• It is a list of finite number of n homogeneous data
elements(i.e. elements of same type) such that:
(a)Elements are sorted in successive memory locations.
(b)Elements are referenced by an index set consisting of
consecutive n numbers.
• Length of an array is calculated using formula:
Length = UB – LB + 1
UB: largest index/upper bound
LB: smallest index/lower bound.
• Elements of the array are denoted as: A[1],A[2],…A[N].
• In A[K], ‘K’ is called subscript or index and ‘A[K]’ is called
subscripted variable.
• Example:

The array data is represented as:


• Finding length of an array:
Representation of Linear Array in Memory

• If LA is a linear array in memory, the location of any


element in LA is denoted as:
LOC(LA[K]) = address of element LA[K] of the array LA.

• Address of 1st element of array LA is denoted:


Base(LA)
called Base Address of an array.
• LOC (LA [K]) = Base (LA) + w (K – LB)
Here LOC (LA [K]) is the location of the Kth
element of LA.
Base (LA) is the base address of LA.
w is the number of bytes taken by one
element.
K is the Kth element.
LB is the lower bound.
Traversing Linear Array
• Suppose, we want to print the contents of file A or we want to
count the number of elements in A with given some priority, it is
done by TRAVERSAL.
• Traversing: Accessing and processing each element of an array.
• ALGORITHM:
Here LA is a linear array with lower bound LB and upper bound UB.
This algorithm traverses LA applying an operation PROCESS to each
element of LA.
1. Set i := LB. [Initialize counter]
2. Repeat for i =LB to UB.
3. Apply PROCESS to LA[i]. [Visiting element]
[End of loop]
4. Exit.
Alternative method of traversing
(Traversing a Linear Array) Here LA is a linear array with lower
bound LB and upper bound UB. This algorithm traverses LA
applying an operation PROCESS to each element of LA.

1. Set i := LB. [Initialize counter]


2. Repeat steps 3 and 4 while i <= UB.
3. Apply PROCESS to LA[i]. [Visit element]
4. Set i := i + 1. [Increment counter]
[End of Step 2 loop]
5. Exit.

* Complexity: O(n)
Example 1

(a)Find number of years(NUM) during which more than 300


automobiles were sold.

1. Set NUM=0. [Initialization]


2. Repeat for K=1932 to 1984:
If AUTO[K]> 300, then:
Set NUM:=NUM+1
[end of loop]
3. Return
(b) Print each year and number of automobiles sold in that
year.

1. Repeat for K = 1932 to 1984:


Write: K, AUTO[K].
[end of loop]
2. Return.
Inserting in Array

•Insertion: adding new elements into the existing data


collection.
Example
Insertion Algorithm
(Inserting into a linear array) INSERT (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such that K <= N.

ALGORITHM: This algorithm inserts an element ITEM into the Kth position in LA.
Elements are being moved downward to create space for new item.
N: size of an array(say 5) and K: where you want to add item(say 3 rd)

1. Set i := N [Initialize counter]


2. Repeat Steps 3 and 4 while i >= K.
3. Set LA[i+1] := LA[i] [Move ith element downward]
4. Set i := i – 1. [Decrease counter.]
[End of Step 2 loop.]
5. Set LA[K] := ITEM [Insert element.]
6. Set N := N + 1. [Reset Size of array.] //which is 6 now
7. Exit.
* Complexity: O(n)
Example
• A linear array(LA) has 4 elements(A,B,C,D) where
LB=1 and UB=4. Write an algorithm to insert an
element E at 3rd position.

A 1 A 1
B 2 B 2
C 3 E 3
D 4 C 4
5 D 5
6 6
Solution:
Algorithm: To insert an E at 3rd position
Step 1: Set i = N=4 [counter initialization]
Step 2: Repeat step 3 and 4 while[i >=K]
[loop]
where K=3
Step 3: LA[i+1] = LA[i]
Step 4: i = i-1
[end loop]
Step 5: Set LA[3] = E(ITEM)
Step 6: N = N+1 //4 = 5, i.e. increasing the index number

Step 7: EXIT
Deletion Algorithm
(Deleting from a linear array) DELETE (LA, N, K, ITEM)
ALGORITHM: This algorithm deletes the Kth element from LA. Here LA
is a linear array with N elements and K is a positive integer such that
K <= N

Step 1. Set ITEM := LA[K].


Step 2. Repeat for i = K to N – 1
Set LA[i] := LA[i + 1] [Moving (i+1) element upward.]
[End of loop.]
Step 3. Set N := N - 1. [Reset the number N of elements in LA.]
Step 4. Exit.

*Complexity: O(n)
Example
• A linear array(LA) has 4 elements(A,B,C,D) where
LB=1 and UB=4. Write an algorithm to delete an
element B at 2nd position.

A 1
C 2
D 3
4
5
6
Solution:

Algorithm: To delete ‘B’ from 2nd position.

Step 1. Set ITEM := LA[K], where K is 2. [counter initialization]


Step 2. Repeat for i = K to N – 1 // N is 4
Set LA[i] := LA[i + 1] [Moving elements upward.]
/* i= 2 to 3
LA[2]=LA[3]
LA[3]=LA[4] */
[End of loop.]
Step 3. Set N := N - 1. [Reset the array size.]
// N=4-1=3
Step 4. Exit.
Merging of an Array

Suppose A is a sorted list with r elements and B is a


sorted list with s elements. The operation that
combines the element of A and B into a single
sorted list C with n = r + s elements is called
MERGING. C
a
A B b
a x c
b y x
c + z = y
: : z
: : :
r elements s elements :
20
(r+s) elements
• Simple way is to place elements of list B after that
of list A and then apply sorting algorithm on the
entire list.
Example:
What’s Happening!
Lets denote the location of smallest elements of A and B as
NA and NB respectively. Also a pointer PTR which denotes
the location in new array C. Initially, set NA=1; NB=1 and
PTR=1.

At each step, we have to compare A[NA] and B[NB]


and then assign the smaller element to C[PTR].

Also PTR is increment by 1 and either NA is


incremented by 1 or NB is incremented by 1, depending
upon whether new element has come from A or B.
NA= NA+1; NB=NB+1; PTR = PTR+1
Merging Algorithm
Algorithm: Merging (A,R,B,S,C)
Here A and B be sorted arrays with R and S elements respectively. This algorithm
merges A and B into an array C with N=R+ S elements.

Step 1: Set NA=1, NB=1 and PTR=1 [initialize the counters for A,B,C]
Step 2: Repeat while NA <= R and NB <= S:
if A[NA] ≤ B[NB], then:
(a) Set C[PTR] = A[NA] //assign element from A to C
(b) Set PTR = PTR+1 and NA = NA +1 //update
pointers
else
(a) Set C[PTR] = B[NB] //assign elements
from B to C
(b) Set PTR = PTR+1 and NB = NB +1 //update pointers
[End of if structure]
[End of Loop]
23
Merging Algorithm

Step 3: [Assigning remaining elements to C]

Step 4: Exit
24
Complexity of Merge algorithm

• Input consists of n = r + s elements(New list C).


While performing each comparison, an element is
assigned to array C, which has n elements.
• Thus number of comparisons cannot exceed n.

• Therefore, Complexity of merging algorithm is


O(n),i.e. merging algorithm runs in linear time.
Searching in Array

• Searching refers to the operation of finding the


location LOC of ITEM in DATA(i.e. array) or
printing the message that ITEM doesn’t appear in
DATA.
• Searching is said to be “successful” if the ITEM is
present ; otherwise “unsuccessful”.
• There are different searching algorithms, but in
case of an array, it is categorised as linear and
binary search.
A. Linear Search


Algorithm:

Step 1: Set DATA[N+1] = ITEM [insert ITEM at the end of the DATA]
Step 2: Set LOC =1 [counter initialization]
Step 3: Repeat while DATA[LOC]  ITEM: [till location of ITEM is not found]
Set LOC = LOC + 1
[end of loop]
Step 4: [Successful ?] If LOC = N+1, then Set LOC = 0
Step 5: Exit
**

•Complexity: Worst Case- O(n+1)O(n)


Average Case- O((n+1)/2)
Example


Since Paula appears at (N+1) th location, thus Paula is not in the
original array.
B. Binary Search
• During each stage of binary search, search for ITEM is reduced to
segments of the DATA.
• Then, algorithm compares the ITEM with the middle element of the
segment, DATA[MID], where MID is obtained by:
• If DATA[MID] = ITEM, then search is successful and we set LOC=MID;
otherwise new segment of DATA is obtained as:
Algorithm:
Each comparison reduces the sample size in two
halves, therefore maximum number of comparisons
required to locate ITEM is:
or

Thus, Complexity: O(log n)


Example
Important points
• Arrays can be declared as: AUTO(1932: 1984); where AUTO[K] =
value at K.

• Length = UB – LB + 1

• Address of any element of LA is calculated as:


LOC(LA[K]) = Base(LA) + w(K – LB)

** Example: Base(A)=100; w= 4 words/memory cell


Find the address/location of A[15], with array A having LB=5
Ans: 100+4(15-5)=100+40=140

You might also like