You are on page 1of 18

Question Bank with solution-Data Structure and Algorithm

Q.No. – 1. Write an an algorithm for insertion in linear Array.

Q.No.-2. Write an algorithm for deletion in linear Array.

Sol. 1.

Insertion in Array-
Insert(A,N,k,Value)(Inserting at kth location)
1. Set j:=N
2. Repeat steps 3 & 4 while j >=k
3. Set A[j+1]=A[j].
4. Set j:=j-1
5. Set A[k]:= value
6. Set N:=N+1
7. Exit.

Sol. 2.

Deletion in Array-
Delete(A,N,k,Value)(deletion at kth location)
1. Set value:= A[k].
2. Set j:=k.
3. Repeat 3 & 4 while j<=N-1.
4. Set A[j]:=A[j+1].
5. Set J:=j+1.

Q. No. -3. Calculate address of A(4) when base address is 1000 and each element occupies 4 memory cells.

Sol.

LOC(A[k]) = Base(A) + w(k-LB) where w is number of memory cell per element, Base(A) is base address i.e.
address of first element of Array, LB is smallest index used in Array.

For ex. If Base address is 1000 and each element occupies 4 memory cells then address of 4th element is
1000+4(4-1)=1012.

Q./No.- 4. Consider a 25 * 4 matrix array A. Suppose Base(A) = 200 and w=4 memory cells per word. Find
address of A(12,3).
Sol.
Address Calculation:-
LOC(A[j,k])=Base(A) + w[(j-LB)+M(k-LB)] - CMO
LOC(A[j,k])=Base(A) + w[N(j-LB)+(k-LB)] - RMO
M – Total No. of Rows
N – Total No. of Columns

LOC(A[12,3])= 200 + 4[(12-1)+25(3-1)] CMO


= 200 + 4[4(12-1)+3-1)] RMO

Q. No. -5. Three dimensional Array-


Suppose Array A(2:8, -4:1, 6:10), Base(A) - 200, w- 4
Find address of A(5, -1, 8)

Solu:LOC(A[i,j,k)] = Base(A) + w[(E1L2 + E2)L3 + E3] RMO


= Base(A) + w[(E3L2+E2)L1 + E1] CMO

L1=8-2+1=7
L2=1-(-4)+1=6
L3=10-6+1=5
E1=5-2=3
E2=-1-(-4)=3
E3=8-6=2
LOC(A[5,-1,8]) = 200 +4[(18+3)5+2] RMO
= 200+4[(12+3)7+3] CMO

Q. No. -6.. Find relation between index of one and two dimensional array when two dimensional sparse matrix
is stored in one dimensional Array.
Sol. Matrices with a relatively high proportion of Zero entries are called sparse matrices.
Lower Triangular Matrix- All entries above the main diagonal are zero or where nonzero entries can occur on or
below the main diagonal.
Upper Triangular matrix- All entries below the main diagonal are zero or where nonzero entries can occur on or
above the main diagonal.
Tridiagonal Matrix- Non zero entries can only occur on the diagonal or on elements immediately above or below
the diagonal

If we store these matrix into one dimensional array B then to access an element index will be calculated as
L=j(j-1)/2 +k for LTM(1 in I row, 2 in II row , 3 in III ro. So upto j-1 row no. of elements = j(j-1)/2
and upto jth row and kth col j(j-1)/2 + k

L=k(k-1)/2 + j for UTM


L=[3(j-2)+2] + [k-j+1] +1= 2j+k-2 for tridiagonal ( 3 elements in each row except first and last)
Problem:- 7. Suppose A is a sorted array with 200 elements and suppose a given element x appears with the
same probability in any place in A. Find the worst case running timef(n) and the average
caserunning time g(n) to find x in Ausing binary search algorithm.

Sol.:- For any value of k let Nk denote the number of those elements in A that will require k comparisons to be
located in A. Then :

K: 1 2 3 4 5 6 7 8
Nk: 1 2 4 8 16 32 64 73
The 73 comes from the fact tjhat 1+2+4+….+64=127 so there are only 200-127=73 elements left. The worst case
sunning timef(n)=8. The average case running time g(n) is obtained as follows.
G(n)=1/n (Summation of k*Nk) k from 1 – 8

=(1*1+2*2+3*4+4*8+5*16+6*32+7*64+8*73)/200 = 6.765

Problem – 8.- Consider a stack of characters, where STACK is allocated N=8 memory cells:
STACK: A, C, D, F, K, __, __, __
Describe the Stack when following operations take place.
a) POP(Stack, value)
b) Pop(Stack, value)
c) Push(Stack, L)
d) Push (Stack, P)
e) POP (Stack, value)
f) Push(Stack, R)
g) Push( Stack, S)
h) Pop (Stack, value)

Sol.
a) STACK: A, C, D, F, __, __, __, __
b) STACK: A, C, D, __, __, __, __, __
c) STACK: A, C, D, L, __, __, __, __
d) STACK: A, C, D, L, P, __, __, __
e) STACK: A, C, D, L, __, __, __, __
f) STACK: A, C, D, L, R, __, __, __
g) STACK: A, C, D, L, R, S, __, __
h) STACK: A, C, D, L, R, __, __, __
Question- 9. Suppose STACK is allocated N=6 memory cells and initially stack is empty, or in other words
TOP=0. Find the output of following module.
1. Set AAA:=2 and BBB:=5
2. Call PUSH(STACK,AAA).
Call PUSH(STACK,4).
Call PUSH(STACK,BBB+2).
Call PUSH(STACK,9).
Call PUSH(STACK, AAA+BBB).
3. Repeat while TOP<>0
Call POP(STACK, value).
Write: value.
4. Return.

Sol. Step 1. Set AAA=2 and BBB = 5.


Step 2. Push AAA=2, 4, BBB+2= 7, 9 and AAA+BBB=7 onto STACK yielding

STACK: 2, 4, 7, 9, 7, _

Step 3. Pop and print the elements of STACK until STACK is empty. Since top element is always popped,
Output consists of the following sequence:
7, 9, 7, 4, 2

Question:- 10. Suppose a given space S of N contiguous memory cells is allocated to K=6 stacks. Describe ways
that the stacks may be maintained in S.

Sol. Suppose no prior data indicates that one stack will grow more rapidly than any of the other stacks. Then one
may reserve N/K cells for each stack.Alternatively, one can partition the stacks into pairs and reserve 2N/K cells
for each pair if stacks. The second method may decrease the number of times overflow will occur.

Problem11- Consider following Arithmetic expression. Convert it into equivalent postfix expression P.
Q: A+(B*C-(D/E^F)*G)*H
Solution:
A+(B*C-(D/E^F)*G)*H)

Symbol Scanned Stack P


(
A ( A
+ (+ A
( (+( A
B (+( AB
* (+(* AB
C (+(* ABC
- (+(- ABC*
( (+(-( ABC*
D (+(-( ABC*D
/ (+(-(/ ABC*D
E (+(-(/ ABC*DE
^ (+(-(/^ ABC*DE
F (+(-(/^ ABC*DEF
) (+(- ABC*DEF^/
* ( +( - * ABC*DEF^/
G (+(-* ABC*DEF^/G
) (+ ABC*DEF^/G*-
* (+* ABC*DEF^/G*-
H (+* ABC*DEF^/G*-H
) ABC*DEF^/G*-H*+

Question:-1 2. Translate by inspection and hand each infix expression into its post fix expression.
a) (A-B)*(D/E) = [AB-]*[DE/]=AB-DE/*
b) (A+B^D)/(E-F)+G=(A+[BD^])/[EF-]+G=[ABD^+]/[EF-]+G=[ABD^+EF-/]+G=ABD^+EF-/G+
c) A*(B+D)/E-F*(G+H/K)=A*[BD+]/E-F*(G+[HK/])=[ABD+*]/E-F*[GHK/+]=[ABD+*E/]-[FGHK/
+*]=ABD+*E/FGHK/+*-

Q.No. – 13. Find A(1,3) using Ackermann Function.

Sol.
1. A(1,3)=A(0,A(1,2))
2. A(1,2)=A(0,A(!,1))
3. A(1,1)=A(0,A(1,0))
4. A(1,0)=A(0,1)
5. A(0,1)=1+1=2
6. A(1,0)=2
7. A(1,1)=A(0,2)
8. A(0,2)=2+1=3
9. A(1,1)=3
10. A(1,2)=A(0,3)
11. A(0,3)=3+1=4
12. A(1,2)=4
13. A(1,3))=A(0,4)
14. A(0,4)=4+1=5
15. A(1,3)=5

Q.No. – 14. Write an algorithm to delete an element in a Queue.


Sol.

Qdelete(Queue,N,Front,Rear,Value)
1. If Front = NULL, then: write: UNDERFLOW and Return.
2. Set Value:=Queue[Front].
3. If Front = rear, then: Set Front:=NULL and Rear:= NULL>
Else If Front=N then:
Set Front:=1.
Else:
Set Front:=Front+1.
4. Exit.

Q.No. -15, what is Deque.

Deques:- Also called doubly ended queues. Elements can be inserted and deleted at both ends but not in middle
of queues. End pointers are denoted by LEFT and RIGHT. In Input restricted deques insertion can take place at
one end from RIGHT(Rear) but deletions can take place at both ends. In output restricted deques deletion can take
place at only one end called LEFT(Front) but insertion can take place at both ends.

Example:- Consider following deque of characters where DEQUE ius a circular array which isallocated six
memory cells.
LEFT:2, RIGHT:4 DEQUUE: __, A, C, D, __ __
Describe the deque while following operations take place.
a) F is added on right
LEFT =2, RIGHT=5 DEQUE:__, A, C, D, F,__
b) Two elements are deleted from right
LEFT=2,RIGHT=3 DEQUE: __ A, C, __, __, __
c) K,L,M are added on left
LEFT=5, RIGHT=3 DEQUE: K, A, C, __, M, L
d) M is deleted
LEFT=6,RIGHT=3 DEQUE: K, A, C, __, __, L
e) R is added on left
LEFT=5, RIGHT=3 DEQUE: K, A, C, __, R, L
f)S is added on right
LEFT=5, RIGHT=4, DEQUE: K, A, C, S, R, L
g) T is added to right
Since LEFT=RIGHT + 1, DEQUE is full, T cannot be added. OVERFLOW Condition.
Question:16Suppose each data structure is stored in a circular array with N memory cells.
a) Find no. of elements NUM in a queue in terms of FRONT and REAR.
b) Find the no. of elements NUM in deque in terms of LEFT & RIGHT.
c) When will the array be filled?
Sol.
a) If FRONT <= REAR, then NUM = REAR – FRONT + 1
If REAR < FRONT, then FRONT – REAR + 1 is no. of empty cells so NUM=N-(FRONT-REAR-1)= N +
REAR-FRONT + 1

Using arithmetic modulo N, we need only one foremula


NUM=REAR-FRONT+1(mod N)
b) NUM=RIGHT-LEFT+1(mod N)
c) With a queue when FRONT =1 and REAR = N or FRONT = REAR +1
With a dequeue when LEFT = 1 and RIGHT = N or LEFT + RIGHT +1
Each of these conditions implies NUM = N

Q.No. 17. Algorithm for insertion in sorted L.L.

Sol. Ins(INFO,LINK,START,AVAIL,LOC,value)

1. if AVAIL = NULL, then: Write: OVERFLOW and Exit.


2. Set NEW:= AVAIL and AVAIL:= LINK[AVAIL].
3. Set INFO[NEW]:= value.
4. if LOC=NULL, then: Set LINK[NEW]:=START and START:=NEW
Else:
Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:=NEW.
5. Exit
Finda(INFO,LINK,START,value,LOC)
1. if START = NULL, then: Set LOC:= NULL and Return
2. if value < INFO[START, thenm: Set LOC:=NULL and Return.
3. Sewt SAVE:=START and ptr:=LINK[START].
4. Repeat steps 5 & 6while ptr<>NULL.
5. if value< INFO[ptr], then: Set LOC:=SAVE and Return.
6. Set SAVE:=ptr and ptr:=LINK[ptr].
7. Set LOC:=SAVE.
8. Return
INSERT(INFO,LINK,START,AVAIL,value)
1. Call Finda(INFO,LINK,START,value,LOC)
2. Call Ins(INFO,LINK,START,AVAIL,LOC,value)
3. Exit.
Q. No.18. Algo for deletion.
Deletion in LinkedList-
Deleting a node following a giving node-
Del(INFO,LINK,START,AVAIL,loc,locp)
1. If locp = NULL, then:
Set START:=LINK[START].
Else:
Set LINK[locp]:=LINK[loc].
2. Set LINK[loc]:=AVAIL and AVAIL:= loc.
3. Exit.

Q.No. -19. Write an Algorithm for preorder Traversal of Binary Tree.


Sol. Pre(INFO,LEFT,RIGHT,ROOT)
1. Set Top:=1, STACK[1]:= NULL, ptr := ROOT.
2. Repeat steps 3 to 5 while ptr <> NULL
3. Apply process to INFO[ptr].
4. if RIGHT[ptr] <> NULL, then: Set Top:= Top + 1, Stack[Top]:= RIGHT[ptr].
5. if LEFT[ptr] <> NULL, then: Set ptr:= LEFT[ptr]
Else :
Set ptr:= STACK[Top], Top:= Top – 1.
6. Exit.

Q. No. -20. Writa an algo for inorder traversal of Binary Tree..


In(INFO,LEFT,RIGHT,ROOT)
1. Set Top:=1, STACK[1]:= NULL, ptr:= ROOT
2. Repeat while ptr <> NULL:
a) Set Top:= Top + 1, STACK[Top]:=ptr.
b) Set ptr:= LEFT[ptr].
3. Set ptr:= STACK[Top], Top:=Top-1
4. Repeat steps 5 to 7while ptr <> NULL:
5. Apply process to INFO[ptr].
6. if RIGHT[ptr] <> NULL, then:
a) Set ptr:=RIGHT[ptr].
b) Goto step 2.
7. Set ptr:= STACK[Top], Top:= Top + 1.
8. Exit.
Q. No. -21. Suppose a function
L = 0, if n=1
= L(floor (n/2)+1) if n>1
a)_ Find L(25)
b) What does this function do?
Sol.
a) L(25)=L(12)+1
=[L(6)+1]+1=L(6)+2=[L(3)+1]+2=L(3)+3
=[L(1)+1]+3=L(1)+4
0+4=4

b) Each time n is divided by 2 the value of L is increased by 1. Hence L is greatest integer such that 2 L
<= n
Accordingly this function finds
L=log2 n

Q. No. -22. Suppose Fibonacci number F11 = 89 and F12 = 144 are given

a) Should one use recursion or iteration to obtain F16? Find16.


b) Write an iterative procedure to obtain first N elements
Sol.
a) Fibonacci numbers should be evaluated by using iteratuion( bottom up) rather than by using
recursion(Top to Bottom) Hence F16 377+610=987

b) FIBONACCI(F,N)

1. Set F[1]:=1 and F[2]:=1


2. Repeat for L=3 to N:
Set F[L]:=F[L-1]+F[L-2].
3. Return.

Q. No. -23.Consider a deque maintained by a circular arrayw with N memory cells.


a) Suppose a element is added to the deque. How is LEFT or RIGHT changed?
b) Suppose an element is deleted. How is LEFT or RIGHT changed?
Sol.
a) If the element is added on left, then LEFT is decreased by 1(mod N). if added on right, right is increased by
1(mod N).
b) If deleted from left then LEFT is increased by 1(mod N).if deleted from right, RIGHT is decreased by 1(mod
N). in the case LEFT=RIGHT before deletion both LEFT & RIGHT are assigned NULL to indicate deque is
empty.

Q. No.-24. Suppose S is the following list of 14 alphabetic characters:


DATASTRUCTURES
Use Quicksort algorithm to find final position of first character D.
Follow Algorithm
1. CATASTRUDTURES
2. CADASTRUTTURES
3. CAADSTRUTTURES
4. CAA D STRUTTURES

Sol. a) Since order in which subsets are sorted does not matter, LOWER and UPPER can be implemented as
queues or even deques rather the stacks

Q. No. -25. Suppose S consists of the following n=5 letters:


A B C D E
Find no. of comparisons to sort S using Quicksort. Draw general Conclusion.
Beginning with E it takes n-1=4 comparisons to recognize that A is already in its correct position. Sorting S is
now reduced to sorting the following sublist with n-1=4 letters.
A B C D E
Beginning with Eit takes n-2 = 3 comparisons to recognize that B in sublist is already in its correct position.
Sorting is now reduced to sorting the following sublist with n-2=3 letters:
A B C D E
Consequently we have 4 + 3 + 2 + 1 =10

So using Quicksort it takes


C=(n-1) + (n-2) + )n-3) + …. + 2 + 1 = n(n-1)/2 = O(n2)

Q. No. -26. Consider Quick sort Algo.


a) Can arrays LOWER and UPPER be implemented as queues rather than stacks?
b) what is space complexity?

Sol. b) Quick sort Algorithm is an in-space algorithm that is elements remain in their places except for
interchanges. The extra space required mainly for stacks

LOWER and UPPER. On the average extra space required for algo is proportional to log n, n is no. of element to
be sorted.
Q27)Explain Basic terminology of Data Structure.

Ans.Basic Terminology:-

Data - values or set of values

Data Item /datum – single unit of value

Group item – which are divided into sub items like employee name may be divided into sub
items like first name, middle name, last name

Elementary data item – that can’t be divided into subparts

Entity – something that has certain attributes.

Attributes- are properties that can be assigned some values.

Entity set – Entities with similar attributes.

Information- meaningful or processed data

Q28)What is Data structure and also Criteria that must be taken into consideration
while chosing a Data Structur

Ans:

Study of data structure includes

1)Logical or mathematical model description of the structure


2) Implementation of the structure on a computer
3)Quantitative analysis of the structure which includes determining the amount of memory
needed to store the structure and the time required to process the structure.

In subject data structure and algorithm, data in primary memory are considered while
DBMS deals with data in secondary memory

Data Structure – Logical or mathematical model of a Particular organization of data is


called data structure.
Criteria that must be taken into consideration while chosing a Data Structure- The choice
of a Particular data structure depends on following considerations.
1. First it must be rich enough in structure to mirror the actual relationships of the data in
real world.
2. Structure should be simple enough that one can effectively process the data when
necessary.
3. Sometimes the choice of data structure involves a time-space tradeoff: i.e. by increasing
the amount of space for storing the data, one may be able to reduce the time needed for
processing the data or vice versa.
4. Choice of data structure also depends upon relative frequency with which various
operations are applied.

Q29) what is complexity and explain Big O notation.


Ans.

Complexity:- An algorithm is a well defined list of steps for solving a Particular problem.
The complexity of an algorithm is the function which gives running time and/or space in
terms of input size.
Big O Notation- Suppose M is an algorithm and n is the size of the input data. Clearly the
complexity f(n) of M increases as n increases. It is rate of increase of f(n) that we want to
examine. This is usually done by comparing f(n) with some standard function such as
Log2 n, n, n log 2 n, n2, n3, 2n
One way to compare the function f(n) with these standard function is to use the functional
O notation. Such that
Complexity linear search(O(n)
Binary search O(log n)
Bubble Sort O(n2)
Merge Sort O(n log n)
Other Asymptotic notation for complexity of algorithm-
Omega Notation(Ω)
The Omega notation is used when function g(n) defines a lower bound for the function f(n)
Theta notation(θ)- This is used when the function(n) is bounded both from above and
below by the function g(n)
Little Oh notation(o)- f of n is little oh of g of n iff f(n) = O(g(n)) and f(n)<> Ωg(n))
Q30) Explain various string operation.
Ans:
String Operations-
Substring-

SUBSTRING(‘TO BE OR NOT TO BE’,4,7)= BE OR N


Indexing-
Suppose T contains the text: ‘HIS FATHER IS THE PROFESSOR’
Then INDEX(T, ‘THE’), INDEX(T, ‘THEN’) and INDEX(T. ‘ THE ‘) have the values 7,0,14
respectively.
Concatenation-
Suppose S1 = ‘MARK’ and S2= ‘TWAIN’, then: S1//S2=’MARKTWAIN’
But S1// ‘ ‘ // S2=’MARKTWAIN’.
Length-
LENGTH(‘COMPUTER’)=8
Insert-
INSERT(‘ABCDEFG’, 3, ‘XYZ’)=’ABXYZCDEFG’
Deletion-
DELETE(‘ABCDEFG’, 4,2)=’ABCFG’
Replacement-
REPLACE(‘XABYABZ’, ‘AB’, ‘C’)=’XCYABZ’

Question31- Discuss how strings are stored and various operations that are applied on
strings.
Sol. Refer Lipschutz page 3.2-3.12

Q32.what is array.
Array
SOLU:
Data structure(DS) are classified as linear or non linear. A DS is said to be linear If its
elements form a sequence or linear list or they are at same level like Arrays, Queues,
Linked List. A DS is said to be non linear when its elements do not form a sequence or when
they are at different levels like Trees and Graphs. One way to represent linear relationship
through sequential memory Locations other way by means of pointer.

Various Operations that are applied on any data structure-


a)Traversal- Visiting each element exactly once.
b) Search-Finding the Location of element with a given value or the record with a
given key.
c)Insertion- Adding a new element to the list. Various conditions may be associated with it.
d) Deletion- Removing an element from the list. Various conditions may be
associated with it.
e)Sorting-Arranging elements in some order.
f) Merging-Combining two lists into a single list.

Linear Array :-
Linear Array is a list of finite number n of homogeneous data elements. such that
a)Elements are referenced respectively by an index set consisting of consecutive numbers.
b) Elements are stored respectively in successive memory Locations.
Length = UB-LB+1
Linear Array are called one dimensional arrays because here each element is referenced by
single subscript.
Sixe(3*5) is read as 3 by 5
Representation of Array in Memory-
Elements are stored in successive memory Locations in memory

Computer Memory

Address calculation for single dimensional array-

LOC(A[k]) = Base(A) + w(k-LB) where w is number of memory cell per element, Base(A) is
base address i.e. address of first element of Array, LB is smallest index used in Array.

For ex. If Base address is 1000 and each element occupies 4 memory cells then address of
4th element is 1000+4(4-1)=1012.

Note:- A collection A of elements is said to be indexed If any element of A, which we will cal
A[k] can be Located and processed in a time that is independent of K. Linear Array can be
indexed. Linked List doesn’t have this property.

Q33.Write an algorithm for traversing of Linear Array.

Ans

Traversing Linear Array-

Traverse(A,LB,UB)

1.Set k:=LB.
2.Repeat steps 3 & 4 while k <= UB.
3.Apply process to A[k].
4.Set k:=k+1
5.Exit.--

Insertion in Array-
Insert(A,N,k,Value)(Inserting at kth Location)
8.Set j:=N
9.Repeat steps 3 & 4 while j >=k
10. Set A[j+1]=A[j].
11. Set j:=j-1
12. Set A[k]:= value
13. Set N:=N+1
14. Exit.

Deletion in Array-
Delete(A,N,k,Value)(deletion at kth Location)
6.Set value:= A[k].
7.Set j:=k.
8.Repeat 3 & 4 while j<=N-1.
9.Set A[j]:=A[j+1].
10. Set J:=j+1.
11. Set N:=N-1
12. Exit.
Q34.write an algorithm for Bubble sort.
Ans
Sorting:-
Bubble Sort
Bubble(A,N)
1.Repeat steps 2& 3 for K=1 to N-1.
2.Set PTR:=1.
3.Repeat while PTR<=N-K
a)If A[PTR] > A[PTR+1], then: Interchange A[PTR] and A[PTR+1]
b) Set PTR:=PTR+1.
4.Exit

Complexity of Bubble Sort Algorithm:- Traditionally time for sorting is measured in terms
of number of comparisons.
Total N-1 Pass
Pass 1 N-1 Comparisons
Pass 2 N-2 Comparisons
Pass 3 N-3 Comparisons
------- --------------

Pass N-1 1 comparisons

So total No. of Comparisons- 1 + 2 + 3+ .. . . .. . . + N-1=N(N-1)/2=O(n2)


We can stop comparisons when there is no interchange in any pass. In that case list is
sorted. we have to modify algorithm in following manner,.

Bubble(A,N)
1.Repeat steps 2 -4 for K=1 to N-1.
2.Set PTR:=1 and flag = 1.
3.Repeat while PTR<=N-K
a)If A[PTR] > A[PTR+1], then: Interchange A[PTR] and A[PTR+1] and Set flag:=0
b) Set PTR:=PTR+1.
4.If flag=1, then: goto step 5.
5.Exit
Q35 . Write an algorithm for binary search . and linear search?
Ans.
Searching:-
Linear Search-Linear(A,N,value,LOC)
1.Set LOC :=1
2.Repeat step 3 while LOC <=N and A[LOC]<> Value
3.Set LOC:=LOC+1.
4.If LOC = N+1, then: write: value does not exist.
Else:
Write: value exist at Location LOC
5.Exit.

Complexity:- Best case Complexity =1


Worst case - n

Average case:- Suppose Pk is probability that value appear in A[k] and suppose q is
probability that value does not appear. Since algorithm uses k comparisons when value
appear in A[k], the average number of comparisons is given by
F(n)= 1.p1 + 2.p2 + …… +n.pn+(n+1).q
In Particular suppose q is very small and value appear with equal probability in each
Location then q=0 and pi=1/n.
So F(n) = 1. 1/n + 2. 1/n + ……+ n.1/n + (n+1).0=(1+2+3+4…+n).1/n=n(n+1)/2 *
1/n=(n+1)/2

Binary Search – Prerequisites are data should be sorted and one must have direct access
to mid element before search begins.

Binary(A,LB,UB,Value,LOC)
1.Set Beg:=LB, End:=UB, Mid:=INT((Beg+End)/2)
2.Repeat steps 3 & 4 while Beg<=End and A[Mid]<>Value
3.If Value < A[Mid], then:
Set End:=Mid-1.
Else:
Set Beg:=Mid + 1
4.Set Mid:=INT((Beg+End)/2)
5.If A[Mid] = Value, then:
Set LOC:=Mid.
Else:
Set LOC:=NULL.
6.Exit.

Complexity – Log2 N(Log N with base 2)

Limitations:- It may be difficult to keep data sorted and one may not have access to mid
element.

Q36)Explain Address calculation Equation for two dimenstional array.


Ans.
Multidimensional Array-
Representation of two dimensional Array in memory
1.Column- Major Order :- Elements are stored column wise
2.Row-Major Order:- Elements are stored row wise

Address Calculation:-
LOC(A[j,k])=Base(A) + w[(j-LB)+M(k-LB)] - CMO
LOC(A[j,k])=Base(A) + w[N(j-LB)+(k-LB)] - RMO
M – Total No. of Rows
N – Total No. of Columns

EX. --Consider a 25 * 4 matrix array A. Suppose Base(A) = 200 and w=4 memory cells per
word.
Then LOC(A[12,3])= 200 + 4[(12-1)+25(3-1)] CMO
= 200 + 4[4(12-1)+3-1)] RMO

Three dimensional Array-


LOC(A[i,j,k)] = Base(A) + w[(E1L2 + E2)L3 + E3] RMO
= Base(A) + w[(E3L2+E2)L1 + E1] CMO

Suppose Array A(2:8, -4:1, 6:10), Base(A) - 200, w- 4


L1=8-2+1=7
L2=1-(-4)+1=6
L3=10-6+1=5
E1=5-2=3
E2=-1-(-4)=3
E3=8-6=2
LOC(A[5,-1,8]) = 200 +4[(18+3)5+2] RMO
= 200+4[(12+3)7+3] CMO

Q37.what is sparse matric.


Ans.
Sparse matrices-
Matrices with a relatively high proportion of Zero entries are called sparse matrices.
Lower Triangular Matrix- All entries above the main diagonal are zero or where nonzero
entries can occur on or below the main diagonal.
Upper Triangular matrix- All entries below the main diagonal are zero or where nonzero
entries can occur on or above the main diagonal.
Tridiagonal Matrix- Non zero entries can only occur on the diagonal or on elements
immediately above or below the diagonal

If we store these matrix into one dimensional array B then to access an element index will
be calculated as
L=j(j-1)/2 +k for LTM
(1 in I row, 2 in II row , 3 in III ro. So upto j-1 row no. of elements = j(j-1)/2 and upto j th row
and kth col
j(j-1)/2 + k

L=k(k-1)/2 + j for UTM


L=[3(j-2)+2] + [k-j+1] +1= 2j+k-2 for tridiagonal ( 3 elements in each row except first and
last)

Problem:- Suppose A is a sorted array with 200 elements and suppose a given element x
appears with the same probability in any place in A. Find the worst case running time f(n)
and the average case running time g(n) to find x in A using binary search algorithm.

Sol.:- For any value of k let Nk denote the number of those elements in A that will require k
comparisons to be Located in A. Then :

K: 1 2 3 4 5 6 7 8
Nk: 1 2 4 8 16 32 64 73
The 73 comes from the fact that 1+2+4+….+64=127 so there are only 200-127=73 elements
left. The worst case sunning time f(n)=8. The average case running time g(n) is obtained as
follows.
G(n)=1/n (Summation of k*Nk) k from 1 – 8

=(1*1+2*2+3*4+4*8+5*16+6*32+7*64+8*73)/200 = 6.765

You might also like