0% found this document useful (0 votes)
31 views

Algo 1

The document describes various algorithms and their properties. It discusses sorting algorithms like insertion sort, bubble sort, selection sort and merge sort. It also covers searching algorithms like binary search. Other topics include recursion, asymptotic notation, and algorithm analysis.

Uploaded by

babu44667788
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Algo 1

The document describes various algorithms and their properties. It discusses sorting algorithms like insertion sort, bubble sort, selection sort and merge sort. It also covers searching algorithms like binary search. Other topics include recursion, asymptotic notation, and algorithm analysis.

Uploaded by

babu44667788
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1. What is Algorithm? with example.

For Example:
One common example of an algorithm is a recipe, which consists of specific instructions for preparing a
dish or meal.

2. Write algorithm to add two numbers given by the user


Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop

3. Write a algorithm to find the average of two numbers given by the user
Step 1 : Start
Step 2 : Input number1, number2
Step 3 : sum = 0 ; average = 0
Step 4 : Calculate sum = (number1 + number2)
Step 5 : Average = sum / 2
Step 6 : Print average of two numbers
Step 7 : Stop

4. Describe algorithm Properties.


Unambiguous − Algorithm should be clear and unambiguous.
Input − An algorithm should have 0 or more well-defined inputs.
Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output.
Finiteness − Algorithms must terminate after a finite number of steps.
Feasibility − Should be feasible with the available resources.
Independent − An algorithm should have step-by-step directions, which should be independent of any
programming code.

5. Briefly explain Priori and Posterior Analysis.


A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an algorithm is measured by
assuming that all other factors, for example, processor speed, are constant and have no effect on the
implementation.
A Posterior Analysis − This is an empirical analysis of an algorithm. The selected algorithm is implemented
using programming language. This is then executed on target computer machine. In this analysis, actual
statistics like running time and space required, are collected.

6. How many aspects of algorithmic performance.


There are two aspects of algorithmic performance:
Time
Instructions take time.
How fast does the algorithm perform?
What affects its runtime?
Space
Data structures take space
What kind of data structures can be used?
How does choice of data structure affect the runtime?

7. What is Time Factor and Space Factor?


Time Factor − Time is measured by counting the number of key operations such as comparisons in the
sorting algorithm.
Space Factor − Space is measured by counting the maximum memory space required by the algorithm.

8. Write algorithm to find the maximum value of 5 number, numbers given by


the user

9. Explain algorithm growth Rates.


Growth Rates
The growth rate for an algorithm is the rate at which the cost of the algorithm grows as the size of its input
grows. The following figure shows a graph for six equations, each meant to describe the running time for a
particular program or algorithm.

10. What is insertion sort explain with example?


Insertion sort is a very simple method to sort numbers in an ascending or descending order.

Example:
11. What is complexity of algorithm with example?
The level in difficulty in solving mathematically posed problems as measured by
- The time
(time complexity)
- number of steps or arithmetic operations
(computational complexity)
- memory space required
(space complexity)
For Example: time complexity for Linear search can be represented as O(n) and O(log n) for Binary search
(where, n and log(n) are the number of operations).

12. What is Asymptotic Notation With Graph?


Asymptotic notations are the mathematical notations used to describe the running time of an algorithm when
the input tends towards a particular value or a limiting value.

For example: In bubble sort, when the input array is already sorted, the time taken by the algorithm is linear
i.e. the best case.

13. Explain bego notation with graph

A Set of all functions whose rate of growth is the same as or lower than that of g(n).
14. Write a algorithm to find the factorial of any number given by the user
Step 1: Start
Step 2: Read a number n
Step 2: Initialize variables:
i = 1, fact = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate
fact = fact * i
Step 5: Increment the i by 1 (i=i+1) and go to step 3
Step 6: Print fact
Step 7: Stop

15. Write algorithm to print the fabonic series.

 Start
 Declare variables i, a,b , show
 Initialize the variables, a=0, b=1, and show =0
 Enter the number of terms of Fibonacci series to be printed
 Print First two terms of series
 Use loop for the following steps
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
 End

16. Explain Thita-notation with graph


A Set of all functions that have same rate of growth as g(n).
17. Explain omega notation with graph
A Set of all functions whose rate of growth is the same as or higher than that of g(n).

18. What is Recursive Function? Shortly Explain Base criteria and Progressive
approach.
A function which is calling itself for a finite time is called recursive function.

Base criteria − There must be at least one base criteria or condition, such that, when this condition is met
the function stops calling itself recursively.

Progressive approach − The recursive calls should progress in such a way that each time a recursive call is
made it comes closer to the base criteria.

19. Write algorithm to find the factorial ot any number using the recursive
Function
Step 1: Start
Step 2: Read a number n
Step 2: Initialize variables:
i = 1, fact = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate
fact = fact * i
Step 5: Increment the i by 1 (i=i+1) and go to step 3
Step 6: Print fact
Step 7: Stop

20. How many methods for Solving Recurrences?

1. Iteration method
2. Substitution method
3. Recursion tree method
4. Master method

1. What is Sorting? Briefly explain types of sorting


Insertion sort is a very simple method to sort numbers in an ascending or descending order.

types of sorting:

1. Insertion sort
Insertion sort is a sorting algorithm that builds a final sorted array (sometimes called a list) one element at
a time. While sorting is a simple concept, it is a basic principle used in complex computer programs such as
file search, data compression, and path finding. Running time is an important thing to consider when
selecting a sorting algorithm since efficiency is often thought of in terms of speed. Insertion sort has an
average and worst-case running time of O(n^2)O(n2), so in most cases, a faster algorithm is more
desirable.

2. Bubble sort
Bubble sort algorithm, also known as sinking sort, is the simplest sorting algorithm that runs through the
list repeatedly, compares adjacent elements, and swaps them if they are out of order.
The process of traversing the list is repeated until the list is sorted.

3. selection sort
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering
ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two
subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.

4. Merge Sort

Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and
Conquer. Merge sort repeatedly breaks down a list into several sublists until each sublist consists
of a single element and merging those sublists in a manner that results into a sorted list.

2. Write a algorithm to find the factorial of any number given by the user
Step 1: Start
Step 2: Read a number n
Step 2: Initialize variables:
i = 1, fact = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate
fact = fact * i
Step 5: Increment the i by 1 (i=i+1) and go to step 3
Step 6: Print fact
Step 7: Stop

3. Write a algorithm to print the fabonic series.

 Start
 Declare variables i, a,b , show
 Initialize the variables, a=0, b=1, and show =0
 Enter the number of terms of Fibonacci series to be printed
 Print First two terms of series
 Use loop for the following steps
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
 End

4. Write a algorithm traverse the whole array

 Step 01: Start


 Step 02: [Initialize counter variable. ] Set i = LB.
 Step 03: Repeat for i = LB to UB.
 Step 04: Apply process to arr[i].
 Step 05: [End of loop. ]
 Step 06: Stop

5. Write a algorithm used in Binary-Search


 Step 1 - Read the search element from the user.
 Step 2 - Find the middle element in the sorted list.
 Step 3 - Compare the search element with the middle element in the sorted list.
 Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function.
 Step 5 - If both are not matched, then check whether the search element is smaller or larger than
the middle element.
 Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left
sublist of the middle element.
 Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right
sublist of the middle element.
 Step 8 - Repeat the same process until we find the search element in the list or until sublist contains
only one element.
 Step 9 - If that element also doesn't match with the search element, then display "Element is not
found in the list!!!" and terminate the function.

6. What is Insertion sort? Write a algorithm of Insertion sort.


Define:
Insertion sort is a very simple method to sort numbers in an ascending or descending order.

Step 1 − If it is the first element, it is already sorted. return 1;


Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted

1. Algrorithm _________ is a method for solving a problem or doing a task.


A. Step-by-step method
B. Step to another method
C. Differenet method
D. Particular method
Ans:A

2. How to algorithm computational procedure that takes collection of elements?


A. Output - algorithm - input
B. Input - algorithm - output
C. Algorithm - input - ouput
D. Algorithm - ouput - input
Ans:B

3. Solve Sorting problem


Ex. Input: sequence 31, 41, 59, 26, 41, 58
Output: sequence is?
A. 31, 41, 58, 26, 41, 59
B. 31, 41, 41, 59, 58, 26
C. 26, 31, 41, 41, 58, 59
D. 31, 41, 42, 59, 59, 26
Ans:C

4. Given an unsorted array. The array has this property that every element in array is at
most k distance from its position in sorted array where k is a positive integer smaller than
size of array. Which sorting algorithm can be easily modified for sorting this array and what
is the obtainable time complexity?
A. Insertion Sort with time complexity O(kn)
B. Heap Sort with time complexity O(nLogk)
C. Quick Sort with time complexity O(kLogk)
D. Merge Sort with time complexity O(kLogk)
Ans:B

5. The recurrence relation capturing the optimal time of the Tower of Hanoi problem with
n discs is. (GATE CS 2012)
A. T(n) = 2T(n – 2) + 2
B. T(n) = 2T(n – 1) + n
C. T(n) = 2T(n/2) + 1
D. T(n) = 2T(n – 1) + 1
Ans:D

6. Most basic and popular algorithms are


A. Unambiguous
B. Input and Output
C. Sorting and Searching
D. Divide-and-Conquer
Ans:C

7. How many inputs of algorithm properties have?


A. 0 or more well-defined inputs.
B. 1 or more well-defined inputs.
C. 2 or more well-defined inputs.
D. 3 or more well-defined inputs.
Ans:A

8. How many output of algorithm properties have?


A. 0 or more well-defined outputs.
B. 1 or more well-defined outputs.
C. 2 or more well-defined outputs.
D. 3 or more well-defined outputs
Ans.B
9. How many aspects of algorithmic performance:
A. 2
B. 4
C. 6
D. 8
Ans:A

10. __________ is measured by counting the number of key operations such as


comparisons in the sorting algorithm.
A. Space Factor
B. Sequence Factor
C. Cost Factor
D. Time Factor
Ans:D

11. _______ is measured by counting the maximum memory space required by


the algorithm.
A. Space Factor
B. Sequence Factor
C. Cost Factor
D. Time Factor
Ans:A

12. Simple If-Statement


Cost Times
if (n < 0) c1 1
absval = -n c2 1
else
absval = n; c3 1

A. Total Cost <= c1 + max(c2,c3)


B. Total Cost >= c1 + max(c2,c3)
C. Total Cost <= c2 + max(c1,c3)
D. Total Cost <= c3 + max(c2,c1)
Ans:A

13. Simple Loop


Cost Times
i = 1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
i = i + 1; c4 n
sum = sum + i; c5 n
}
A. Total Cost = c2+ c2 + (n+1)*c3 + n*c4 + n*c1
B. Total Cost = c1 + c3 + (n+1)*c3 + n*c5 + n*c5
C. Total Cost = c1 + c2 + (n+1)*c4 + n*c2 + n*c1
D. Total Cost = c1 + c2 + (n+1)*c3 + n*c4 + n*c5
Ans:D

14. The Common Growth Rates Constant is:


A. e
B. N
C. c
D. D
Ans:C
15. The Common Growth Rates Linear is:
A. e
B. N
C. c
D. D
Ans:B
16. The Common Growth Rates Logarithmic is:
A. log N
B. N
C. log2N
D. log
Ans:A
17. What is the running time of an insertion sort algorithm if the input is
pre-sorted?
A. O(N2)
B. O(N log N)
C. O(N)
D. O(M log N)
Ans:C
18.The _________ algorithm iterates through an input array and removes one
element per iteration, finds the place the element belongs in the array, and
then places it there.
A. Insertion sort
B. Asymptotic Notations
C. Recursive Algorithms
D. Case study
Ans:A
19. How many things about a loop invariant:
A. 5
B. 6
C. 3
D. 2
Ans:C
20. Which of the following real time examples is based on insertion sort?
A. arranging a pack of playing cards
B. database scenarios and distributes scenarios
C. arranging books on a library shelf
D. real-time systems
Ans:A
21. Binary search can be used in an insertion sort algorithm to reduce the
number of comparisons.
A. True
B. False
Ans:A
22. For the best case input, the running time of an insertion sort algorithm is?
A. Linear
B. Binary
C. Quadratic
D. Depends on the input
Ans:A
23. Which of the following examples represent the worst case input for an
insertion sort?
A. array in sorted order
B. array sorted in reverse order
C. normal unsorted array
D. large array
Ans:B
24. How many passes does an insertion sort algorithm consist of?
A) N
B) N-1
C) N+1
D) N2
Ans:B
25. ________ is the amount of time taken by an algorithm to run, as a function
of the length of the input.
A. The time
B. Time complexity
C. space complexity
D. arithmetic operations
Ans:B
26. ________ of an algorithm refers to defining the mathematical
boundation/framing of its run-time performance.
A. Symptotic analysis
B. Asymptotic analysis
C. Posterior Analysis
D. Priori Analysis
Ans:B
27. __________ is the formal way to express the upper bound of an algorithm's
running time.
A. Omega Notation
B. Theta Notation
C. Big Oh Notation
D. All of the above
B. Ans : C
28. Which of the following is linear asymptotic notations?
A. Ο(1)
B. Ο(log n)
C. Ο(n)
D. Ο(n log n)
Ans : C
29. An equation or inequality that describes a function in terms of its value on
smaller inputs as:
A. T(n) = T(n-2) + T
B. T(n) = T(T-1) + n
C. T(n) = N(n-1) + n
D. T(n) = T(n-1) + n
Ans:D

30. A function which is calling itself for a finite time is called……….


A. Base criteria
B. Progressive approach
C. Recursive function
D. Running Time
Ans:C
31. What is the worst case complexity of binary search using recursion?
A. O(nlogn)
B. O(logn)
C. O(n)
D. O(n2)
Answer:B

32. The Methods for Solving Recurrences is The Iteration Method is used for:
A. Iterate the recurrence until the initial condition is reached.
B. Use induction to prove that the solution works
C. Each node represents the cost incurred at various
D. Sum up the costs of all levels
Ans:A

33. The Methods for Solving Recurrences is The recursion-tree method is used
for:
A. Iterate the recurrence until the initial condition is reached.
B. Use induction to prove that the solution works
C. Each node represents the cost incurred at various
D. Sum up the costs of all levels
Ans:D
34. The approach of dynamic algorithm is similar to
A. Parsing
B. Hash table
C. Divide and Conquer algorithm
D. Greedy algorithm
Ans:C
Answer

35. The algorithms like merge sort, quick sort and binary search are
based on
A. Greedy algorithm
B. Divide and Conquer algorithm
C. Hash table
D. Parsing
Ans:B
Ans

36. In the Divide and Conquer process, breaking the problem into smaller
sub-problems is the responsibility of
A. Divide/Break
B. Sorting/Divide
C. Conquer/Solve
D. Merge/Combine
Ans:A
37. Quick sort follows Divide-and-Conquer strategy.
A. True
C. False
Ans: A

38. ind the pivot element from the given input using median-of-three
partitioning method.
8, 1, 4, 9, 6, 3, 5, 2, 7, 0.
A. 8
B. 7
C. 9
D. 6
Answer: D
39. Which of the following is not a variant of merge sort?
A. in-place merge sort
B. bottom up merge sort
C. top down merge sort
D) linear merge sort
Ans:D
40. Which of the following sorting algorithm does not use recursion?
A. quick sort
B. merge sort
C. heap sort
D) bottom up merge sort
Ans:D

You might also like