Professional Documents
Culture Documents
Question Bank With Solution-Data Structure and Algorithm: Insertion in Array
Question Bank With Solution-Data Structure and Algorithm: Insertion in 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
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
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.
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)
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/+*-
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
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.
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
Sol. Ins(INFO,LINK,START,AVAIL,LOC,value)
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
b) FIBONACCI(F,N)
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
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:-
Group item – which are divided into sub items like employee name may be divided into sub
items like first name, middle name, last name
Q28)What is Data structure and also Criteria that must be taken into consideration
while chosing a Data Structur
Ans:
In subject data structure and algorithm, data in primary memory are considered while
DBMS deals with data in secondary memory
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-
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.
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
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.
Ans
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
------- --------------
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.
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.
Limitations:- It may be difficult to keep data sorted and one may not have access to mid
element.
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
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
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