Algo 1
Algo 1
For Example:
One common example of an algorithm is a recipe, which consists of specific instructions for preparing a
dish or meal.
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
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).
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.
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
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
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
1. Iteration method
2. Substitution method
3. Recursion tree method
4. Master method
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
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. 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
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