0% found this document useful (0 votes)
4 views22 pages

3 Arrays Copy

The document provides a comprehensive overview of linear arrays, covering basic terminology, memory representation, and algorithms for traversing, inserting, deleting, merging, and searching within arrays. It includes detailed explanations of linear and binary search methods, along with their limitations. Additionally, the document presents algorithms for various operations on arrays and poses review questions for understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

3 Arrays Copy

The document provides a comprehensive overview of linear arrays, covering basic terminology, memory representation, and algorithms for traversing, inserting, deleting, merging, and searching within arrays. It includes detailed explanations of linear and binary search methods, along with their limitations. Additionally, the document presents algorithms for various operations on arrays and poses review questions for understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Data Structures

Lecture : Linear Array

By
Amit Sharma
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Basic Terminology
• Linear Array
• Memory Representation of Linear Array
• Traversing Array
• Insertion and Deletion in Array
• Sorting (Bubble Sort)
• Searching (Linear Search and Binary Search)
• Review Questions
Basic Terminology
• Linear Data Structures: A data structure is said to be linear if its
elements form a sequence or a linear list.

• Linear Array: is a list of a finite number n of homogeneous data


elements such that:

(a) the elements of the array are referenced by an index set


consisting of n consecutive numbers.

(b) the elements of the array are stored respectively in


successive memory locations.
Key Terms
• Size / Length of Array
• Index of Array
• Upper bound and Lower bound of Array
Memory Representation of Arrays
19 5 42 18 199

1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
Implementation of Linear Array in
Memory

Location of A[K]=BASE[A]+W[K-LB]
BASE[A] - Base address of array.
(The address of the first reserve location for the array is called
Base address of the array.)
K is the position of any element in the array
LB – Lower Bound.
W = 1 for Character.
W = 2 for Integer.
W = 4 for Float.
Question
• An array ‘A’ consist of 5 character elements, Calculate
locations of the elements, The base address in 2001.
A[0],A[1],A[2],A[3],A[4]

• An array ‘A’ consist of 5 integer elements, Calculate


locations of the elements, The base address in 2001.
A[0],A[1],A[2],A[3],A[4]

• An array ‘A’ consist of 5 Float elements, Calculate


locations of the elements, The base address in 2001.
A[0],A[1],A[2],A[3],A[4]
Traversing Linear Array
• Suppose we have to count the number of element is an array
or print all the elements of array.

• Algorithm 1: (Using While Loop)

1. [Initialize Counter.] Set K = LB.


2. Repeat Step 3 and 4 while K<= UB.
3. [Visit Element.] Apply PROCESS to A[K].
4. [Increase Counter.] Set K = K+1.
[End of Step 2 Loop.]
5. Exit.
Algorithm 2: (Using for loop)

1. Repeat for K = LB to UB
Apply PROCESS to A[K].
[ End of Loop.]
2. Exit.

Question.1: Find the Number of elements in an array which are


greater than 25.

Question 2: Find out the sum of all the two digit numbers in an
array.
Insertion and Deletion in an Array
• Two types of insertion are possible:
• Insertion at the end of array
• Insertion in the middle of the array
Insertion at the End of Linear Array

Consider an array ‘A’ that can store maximum of M elements.


At present array is having N number of elements, such that
N<M. To insert an element ‘data’ at the end of array.

1. Dimension of A is M.
2. Repeat for I=0 to N-1.
Read A[I]
3. Set A[N]=data [(N-1)+1]=N
4. Repeat for I=0 to N.
5. END
Insertion into a Linear Array
• Algorithm: (Consider an array ‘A’ that can store maximum of
M elements. At present array is having N number of elements,
such that N<M. To insert an element NEW at the Kth position
Kth place where K<N)

1. [Initialize Counter.] Set J = N-1


2. Repeat Steps 3and 4 while J >= K.
3. [Move Jth element downward] Set A[J+1] = A[J].
4. [Decrease Counter.] Set J = J-1.
[End of Step 2 loop]
5. [Insert element.] Set A[K] = ITEM.
6. [Reset N] N = N+1.
7. Exit
Deletion into a Linear Array
Algorithm: (Delete Kth element from Linear Array A)

1. Set DATA=A[K]
2. Repeat for J = K to N-1.
3. [Move (J+1)th element upward] Set A[J] = A[J+1].
[End of loop.]
4. [Reset the number of elements N] Set N = N-1.
5. Exit
Merging Algorithm

• 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.

14
Merging Algorithm
 Algorithm: Merging (A, R,B,S,C)
Here A and B be sorted arrays respectively. This algorithm merges A and B
into an array C.

 Step 1: Set i=L1, j=L2 and k=0


 Step 2: Repeat while i <= U1 and j <= U2
 Step 3: if A[i] < B[j], then:
Set C[k] = A[i]
Set i=i+1, k=k+1
else
Set C[k] = B[j]
Set j=j+1, k=k+1
[End of if structure]
[End of Loop]
15
Merging Algorithm
 Step 4: while (i <= U1) //only the elements of 1st array left
Set C[k] = A[i]
Set i=i+1, k=k+1
[End of Loop]

while (j <= U2) //only some elements of 2nd array left


Set C[k] = B[j]
Set j = j + 1, k = k +1
[End of loop]

 Step 5: Exit

16
Merging Algorithm
• Complexity of merging: The input consists of the total number
n=r+s elements in A and B. Each comparison assigns an
element to the array C, which eventually has n elements.
Accordingly, the number f(n) of comparisons cannot exceed n:
f(n) ≤ n = O(n)

17
Searching
1. Linear Search:
• Compares the item of interest with each element of Array
one by one.
• Traverses the Array sequentially to locate the desired
item.
Linear Search Algorithm
• LINEAR (DATA, N, ITEM, LOC)

1. [Insert ITEM at the end of DATA]


Set Data [N]= ITEM.
2. [Initialize Counter] Set LOC=0.
3. Repeat while DATA [LOC] != ITEM
Set LOC = LOC +1.
[End of loop.]
4. [Successful?] if LOC = N, then Set LOC = NULL.
5. Exit.
Binary Search
• BINARY ( DATA, LB, UB, ITEM, LOC )
1. [Initialize Segment Variables]
Set BEG = LB, END = UB and MID = INT ((BEG+END)/2).
2. Repeat Steps 3 and 4 while BEG <= END and DATA [MID] != ITEM.
2. If ITEM < DATA[MID], then:
Set END = MID - 1.
Else:
Set BEG = MID + 1.
[End of if Structure.]
3. Set MID = INT ((BEG+END)/2).
[End of Step 2 Loop.]
4. If DATA [MID] = ITEM, then: LOC=MID
Else:
Set LOC = NULL.
[End of if structure.]
5. Exit.
Limitations of Binary Search
• Although the complexity of Binary Search is
O (log n), it has some limitations:
1. the list must be sorted
2. one must have direct access to the middle element in any
sublist.
Questions

You might also like