algo (autosaved).docx

7
 Q.Define algorithm? What are its properties? Ans.  An algorithm is a set of instructions that provide step-by-step specifications to perform a task. The properties of an algorithm are: >Input: Specifies the data set that is applied to the algorithm to check its validity. >Output: Specifies the data set that is produced as a result of the algorithm execution. >Definiteness: Specifies that the instructions described in the algorithm should be well defined and should not create any ambiguity. >Termination: Specifies that the instructions described in the algorithm must contain a proper termination condition. >Effectiveness: Specifies that the algorithm take less time and less memory space during its execution. Q. What is debugging and what is profiling? Ans.Debugging is the process of identifying and fixing the errors in a program. Errors in a program can be identified by executing the program with a sample dataset. Profiling is the process of measuring the performance of the program by executing it on different data sets. Performance of a program is measured by recording the time and memory space that the program takes during its execution. Q. Give at least 5 real life examples where we use stack operations. Ans. The real life examples of stacks are: >Bangles in a hand: The bangles wore in a hand follow last-in-first-out (LIFO) strategy of stack. The bangle that you wear first is the last one to be taken out while removing all the bangles from the hand. The bangle that is worn last is the first one to be taken out. >Same circumference circular rings in a pole : The rings having same circumference placed into a pole also follow LIFO strategy. The topmost ring, which was the last to be placed in the pole, is the first one to be taken out. >Sacks full of wheat placed one over other : The sack at the top is removed first and the sack at the bottom is removed last. >The bolts screwed to a single nut: When the bolts are screwed to a single nut, the last screwed bolt is unscrewed first and the bolt that was screwed first is unscrewed in the last. >Battery cells in a torch: The battery cells in a torch also follow the same LIFO strategy of stack. Q. Design an algorithm to generate all prime numbers within the limits l1 and l2. Ans.  Algorithm: to generate all prime numbers between the limits l1 and l2. Input: l1 and l2 Output: Prime numbers between l1 and l2 Method: for (n=l1 to l2 in steps of 1 do) prime=true for (i=2 to n/2 in steps of 1 do) if (n % i =0) prime = false break end_if end_for if (prime = true) Display 'Prime number is =', n end_for Q. One of the properties of an algorithm is beauty (true/false) Ans.False Ans:-Profilingis the process of executing a correct program on data sets and measuring the time and space it takes to compute the results? Q. Give at least 5 real life examples where queue is used. Ans. Real life examples of queue are: >A queue of people at ticket-window: The person who comes first gets the ticket first. The person who is coming last is getting the tickets in last. Therefore, it follows first-in-first-out (FIFO) strategy of queue. >Vehicles on toll-tax bridge: The vehicle that comes first to the toll tax booth leaves the booth first. The vehicle that comes last leaves last. Therefore, it follows first-in-first-out (FIFO) strategy of queue. >Phone answering system: The person who calls first gets a response first from the phone answering system. The person who calls last gets the response last. Therefore, it follows first-in-first-out (FIFO) strategy of queue. >Luggage checking machine: Luggage checking machine checks the luggage first that comes first. Therefore, it follows FIFO principle of queue. >Patients waiting outside the doctor's clinic : The patient who comes first visits the doctor first, and the patient who comes last visits the doctor last. Therefore, it follows the first-in-first-out (FIFO) strategy of queue. Q. Design and develop algorithms for multiplying n integers. Hint: Follow the algorithm to add n numbers given in the text Ans.Algorithm: Multiply_n_Integers Input: integers, number of integers to be multiplied (n), loop variable (i) Q. Develop an algorithm to find the number of Permutations and Combinations for a given n and r. Ans.Permutation of a given number is given by n*(n- 1)*(n-2)...up to r factors. This is a generalized algorithm for n>2 Algorithm: Permutation of a number for a given r Input: n and r Output: Permutation of n Method: a) per = 1 for (j = n to n - r + 1 in step s of -1 do) //where j is a loop variable per = per*j end_for Display ' Permutation = ', per b) Combination of a number n for a given r is calculated by nCr = nPr / r! Calculate the p ermutation nPr using the abo ve algorithm Calculate the factorial for r using the algorithm fact = 1 for (j =1 to r in steps of 1 do) //where j is a loop variable fact = fact*j end_for comb = per / fact Display ' Combination =', comb Output: nul, updated Method: Display 'Enter the number of elements'  Accept n Display 'Enter elements one by one' for (i = 1 to n i n steps of 1 do)  Accept a (i) end_for mul = 1 for (i = 1 to n in steps of 1 do) mul = mul*a(i) end_for Display 'multiplication of n integers is = ', mul Q.What is characterstic of stack? Mentions its application. Ans:-the characterstic are stack are as follow >data can be inserted on the top of the stack. >data can only deleted from the top of the stack. >data can not be deleted middle of the stack without first removing all item from the top. Stack are simple data structure that play prominent role in many application are method is next point:- >implaementing function cell >maintain the undo list from an application >evaluating an expressio Q. Design and develop an algorithm for finding the middle element in three numbers. Ans.To find the middle element one has to first sort the numbers in ascending order. The smallest number becomes the first ele ment in the sorted list and the largest number becomes the last element in the sorted list. Consider three numbers 3, 1, and 7. The smallest number amongst these three is 1; therefore, 1 becomes the first element in the sorted list. Amongst 3 and 7, 3 is the second element. The larger number 7 is left out and becomes the last element. Hence the middle number 3 is displayed. Algorithm : Middle_3_Elements Input: a, b, c the three numbers to be sorted, two temporary variables k1, and K2 Output: Middle element in the three numbers. Method: If (a<b) If (a<c) If (b<c) m = b else m = c end if else m = a end if else if (b<c) if (a<c) m = a else m = c end if else m = b end if end if Q. Consider a data set of nine elements {10, 30, 45, 54, 56, 78, 213, 415, 500} and trace the linear search algorithm to find whether the keys 30, 150, 700 are present in the data set or not. Ans.In linear search algorithm, each element in the list is compared with the given key element. If any of the elements in the list is equal to t he given key element, the linear search algorithm returns TRUE, else it returns FALSE. Let us apply linear search to find the key element 30 in the given list. >Take the first element 10 from the list and compare it with the key element 30. Clearly the two elements are not equal. >Take the next element 30 from the list and compare it with the key element 30. Clearly, the two elements are equal. The algorithm returns the TRUE value, and the algorithm is terminated. Similarly, search for the key elements 150 and 700.  At the end of the search, you will find that the key elements 150 and 700 are not found in the list. Q. Implement all the algorithms designed in the chapter. Ans:-You can implement algorithms discussed in this chapter in various languages, such as C++ and Java. However, you need to know the syntax of the respective language before implementing it. Q.What is linear data structure. Write short note it Ans- A data structure in which every data element has got exactly two neighbors or two adjacent elements except two elements having exactly one data element is called a linear data structure. Otherwise it is called a nonlinear data structure. Q. Trace the binary search algorithm on the same data set and same key elements of problem 2. Ans:-Binary search algorithm can be applied  to the sorted list of elements. Lets first apply the binary search algorithm to find the key element 30 in the following list taken from p roblem 2: Take the middle element from the list and compare it with the key element. Clearly, the middle element 56 > key element 30. As the key element is smaller than the middle element, the key element can only be present in the left sub list that i s as follows:  Again, take the middle element from this list and compare it with the key element. Clearly, the middle element 45 > key element 30. As the key element is smaller than the middle element, the key element can only be present in the left sub list that i s as follows:  Again, take the middle element from this list and compare it with the key element. Clearly middle element 30= key element 30, therefore, the binary search algorithm returns a TRUE value and the algorithm is terminated. Similarly, search for the key elements 150 and 700.  At the end of the search, you will find that the key elements 150 and 700 are not found in the list. Q. Try to know more sorting techniques and make a comparative study of them. Ans.There are various sorting techniques, such as bubble sort, quick sort, and shell sort. Each of these sorting techniques is defined as follows: >Bubble sort: In the bubble sort technique two elements are compared at a time and if the two elements are not in ascending order these elements are interchanged. This process is repeatedly performed throughout the given list until the list is completely sorted. To sort a list of n elements using bubble sort you need to make a total of (n-1) 2  comparisons. >Quick sort: The basic idea underlying quick sort is to allow a specific element 'a' within the list 'x' to find its proper position 'j'. The proper position 'j' is found such that it satisfies the following two conditions: The elements on the left hand side of position 'j' are all smaller than or equal to 'a' The elements on the right hand side of position 'j' are all greater than or equal to 'a' If 'a' satisfies these two conditions, then 'a' is the jth smallest element in the list and 'a' is placed at jth position in the finally sorted list. This process is then repeated for sub arrays x [0..j-1] and x [j+1..n-1]. >Shell sort: In shell sort, the given list x is divided into sub lists containing every kth element of the given list. For example, if k=5 then one sub list contains x [0], x [5], x [10]..., another sub list contains x [1], x [6], x [11]..., and so on. The elements of these sub lists are then compared two at a time and if the two elements are not in ascending order, these elements are interchanged. Q. What are the serious shortcomings of the binary search method and sequential search method? Ans.:-A serious shortcoming of the sequential search method is that even if the element that you are trying to search is not present in the given file, the entire file is searched at least once.A serious shortcoming of the binary search method is that it can be applied only to a list in which the elements are arranged in ascending order. Q:-design an itcrative algorithmto find a factorial of a number. Ans:- Mathematically represented as n! . For ex: 5! = 5*4*3*2*1.  Algorithm : Factorial Input : n Output : Factorial of n Method fact = 1 for i = n to 1 i n steps of 1 do fact = fact*i end_for display factorial = ,fact Algorithm ends 

Upload: sanju1t

Post on 04-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: algo (Autosaved).docx

8/14/2019 algo (Autosaved).docx

http://slidepdf.com/reader/full/algo-autosaveddocx 1/7

Page 2: algo (Autosaved).docx

8/14/2019 algo (Autosaved).docx

http://slidepdf.com/reader/full/algo-autosaveddocx 2/7

Page 3: algo (Autosaved).docx

8/14/2019 algo (Autosaved).docx

http://slidepdf.com/reader/full/algo-autosaveddocx 3/7

Page 4: algo (Autosaved).docx

8/14/2019 algo (Autosaved).docx

http://slidepdf.com/reader/full/algo-autosaveddocx 4/7

Page 5: algo (Autosaved).docx

8/14/2019 algo (Autosaved).docx

http://slidepdf.com/reader/full/algo-autosaveddocx 5/7

Page 6: algo (Autosaved).docx

8/14/2019 algo (Autosaved).docx

http://slidepdf.com/reader/full/algo-autosaveddocx 6/7

Page 7: algo (Autosaved).docx

8/14/2019 algo (Autosaved).docx

http://slidepdf.com/reader/full/algo-autosaveddocx 7/7