biojuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · web viewq.no. – 1. write...

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

Upload: others

Post on 08-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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:=N2. Repeat steps 3 & 4 while j >=k3. Set A[j+1]=A[j].4. Set j:=j-15. Set A[k]:= value6. Set N:=N+17. 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:-

Page 2: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

LOC(A[j,k])=Base(A) + w[(j-LB)+M(k-LB)] - CMOLOC(A[j,k])=Base(A) + w[N(j-LB)+(k-LB)] - RMOM – Total No. of RowsN – 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- 4Find 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=7L2=1-(-4)+1=6L3=10-6+1=5E1=5-2=3E2=-1-(-4)=3E3=8-6=2LOC(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 asL=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 UTML=[3(j-2)+2] + [k-j+1] +1= 2j+k-2 for tridiagonal ( 3 elements in each row except first and last)

Page 3: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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 8Nk: 1 2 4 8 16 32 64 73The 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, __, __, __

Page 4: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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:=52. 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<>0Call 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)*HSolution:

A+(B*C-(D/E^F)*G)*H)

Symbol Scanned Stack P(

A ( A+ ( + A( ( + ( AB ( + ( A B* ( + ( * A B

Page 5: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

C ( + ( * A B C- ( + ( - A B C *( ( + ( - ( A B C *D ( + ( - ( A B C * D/ ( + ( - ( / A B C * DE ( + ( - ( / A B C * D E ^ ( + ( - ( / ^ A B C * D EF ( + ( - ( / ^ A B C * D E F) ( + ( - A B C * D E F ^ /* ( +( - * A B C * D E F ^ /G ( + ( - * A B C * D E F ^ / G) ( + A B C * D E F ^ / G * -* ( + * A B C * D E F ^ / G * -H ( + * A B C * D E F ^ / G * - H) 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/+*-

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=26. A(1,0)=27. A(1,1)=A(0,2)8. A(0,2)=2+1=39. A(1,1)=310. A(1,2)=A(0,3)11. A(0,3)=3+1=412. A(1,2)=413. A(1,3))=A(0,4)14. A(0,4)=4+1=515. A(1,3)=5

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

Page 6: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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 rightLEFT =2, RIGHT=5 DEQUE:__, A, C, D, F,__b) Two elements are deleted from rightLEFT=2,RIGHT=3 DEQUE: __ A, C, __, __, __c) K,L,M are added on leftLEFT=5, RIGHT=3 DEQUE: K, A, C, __, M, Ld) M is deletedLEFT=6,RIGHT=3 DEQUE: K, A, C, __, __, Le) R is added on leftLEFT=5, RIGHT=3 DEQUE: K, A, C, __, R, Lf)S is added on rightLEFT=5, RIGHT=4, DEQUE: K, A, C, S, R, Lg) T is added to right Since LEFT=RIGHT + 1, DEQUE is full, T cannot be added. OVERFLOW Condition.

Page 7: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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 + 1If 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 foremulaNUM=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 +1With a dequeue when LEFT = 1 and RIGHT = N or LEFT + RIGHT +1Each 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.

Page 8: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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.

Page 9: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

Q. No. -21. Suppose a function L = 0, if n=1 = L(floor (n/2)+1) if n>1a)_ 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)+40+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 <= nAccordingly this function findsL=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 elementsSol.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]:=12. 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).

Page 10: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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:D A T A S T R U C T U R E SUse Quicksort algorithm to find final position of first character D.Follow Algorithm1. C A T A S T R U D T U R E S2. C A D A S T R U T T U R E S3. C A A D S T R U T T U R E S4. C A A D S T R U T T U R E S

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 EBeginning 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 EConsequently 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.

Page 11: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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 structure2) Implementation of the structure on a computer3)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.

Page 12: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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 asLog2 n, n, n log 2 n, n2, n3, 2nOne way to compare the function f(n) with these standard function is to use the functional O notation. Such thatComplexity 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 NIndexing-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’)=8Insert-

Page 13: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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 thata)Elements are referenced respectively by an index set consisting of consecutive numbers.b) Elements are stored respectively in successive memory Locations.Length = UB-LB+1Linear Array are called one dimensional arrays because here each element is referenced by single subscript.Sixe(3*5) is read as 3 by 5

Page 14: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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+15.Exit.--

Insertion in Array-

Page 15: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

Insert(A,N,k,Value)(Inserting at kth Location)8.Set j:=N9.Repeat steps 3 & 4 while j >=k10. Set A[j+1]=A[j].11. Set j:=j-112. Set A[k]:= value13. Set N:=N+114. 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-112. Exit.Q34.write an algorithm for Bubble sort.AnsSorting:-Bubble SortBubble(A,N)1.Repeat steps 2& 3 for K=1 to N-1.2.Set PTR:=1.3.Repeat while PTR<=N-Ka)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 PassPass 1 N-1 ComparisonsPass 2 N-2 ComparisonsPass 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,.

Page 16: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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-Ka)If A[PTR] > A[PTR+1], then: Interchange A[PTR] and A[PTR+1] and Set flag:=0b) Set PTR:=PTR+1.4.If flag=1, then: goto step 5.5.ExitQ35 . Write an algorithm for binary search . and linear search? Ans.Searching:-Linear Search-Linear(A,N,value,LOC)1.Set LOC :=12.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 LOC5.Exit.

Complexity:- Best case Complexity =1Worst 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 byF(n)= 1.p1 + 2.p2 + …… +n.pn+(n+1).qIn 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]<>Value3.If Value < A[Mid], then:Set End:=Mid-1.Else:Set Beg:=Mid + 14.Set Mid:=INT((Beg+End)/2)

Page 17: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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 memory1.Column- Major Order :- Elements are stored column wise2.Row-Major Order:- Elements are stored row wise

Address Calculation:- LOC(A[j,k])=Base(A) + w[(j-LB)+M(k-LB)] - CMOLOC(A[j,k])=Base(A) + w[N(j-LB)+(k-LB)] - RMOM – Total No. of RowsN – 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- 4L1=8-2+1=7L2=1-(-4)+1=6L3=10-6+1=5E1=5-2=3E2=-1-(-4)=3E3=8-6=2LOC(A[5,-1,8]) = 200 +4[(18+3)5+2] RMO

= 200+4[(12+3)7+3] CMO

Page 18: BioJuncturegetenotes.weebly.com/uploads/1/4/1/9/14194145/dsa_qu…  · Web viewQ.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion

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 asL=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 UTML=[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 8Nk: 1 2 4 8 16 32 64 73The 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