Question 1A)
The time factor when determining the efficiency of algorithm is measured by
a) Counting microseconds
b) Counting the number of key operations
c) Counting the number of statements
d) Counting the kilobytes of algorithm
Answer: b
Justification: It is hardware and language independent , rest are dependent on hardware or on software
1B) The concept of order Big O is important because
a) It can be used to decide the best algorithm that solves a given problem
b) It determines the maximum size of a problem that can be solved in a given amount of time
c) It is the lower bound of the growth rate of algorithm
d) Both A and B
Answer: a
Justification: We know that Big Oh notation is used in time complexity . the reason why we called Big Oh because it says "at any condition this is worst time that an algorithm could take " where as Big Omega says its is "best time that you can get , You cant get better than this.
1C) In worst case Quick Sort has order
A) O (n log n) B. O (n2 /2) C. O (log n) D. O (n2 /4)
Answer: A
Justification: In the worst case of quick sort has order O(n2). Quick sort is the quickest comparison-based sorting algorithm. It is very fast and requires less additional space, only O(n log n) space is required.
1D) Consider the following program segment. What is the Space complexity?
Begin
i=0;
S=0;
S=s+1;
Return s;
End
Answer: [If i,S considered as integers] => Space Complexity = 4 +(4*2) = 12 bytes, where additional 4 bytes is for return value
To calculate the Space complexity of an algorithm, we need to follow the below instruction first:
It is the amount of memory used by the algorithm (including the input values to the algorithm) to execute and produce the result.
Space Complexity = Auxiliary Space + Input space
Calculating the Space Complexity:
For calculating the space complexity, we need to know the value of memory used by different type of datatype variables, which generally varies for different operating systems, but the method for calculating the space complexity remains the same.
Type | Size |
---|---|
bool, char, unsigned char, signed char | 1 byte |
short, unsigned short, | 2 bytes |
float, int, unsigned int, long, unsigned long | 4 bytes |
double, long double, long long | 8 bytes |
(1) a)O(n) b) O(1) c)O(2n/2) d) O(n2)
Answer: d
Justification:
An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum.
For Reference:
The complexity of linear search algorithm is
a) O(n)
b) O(log n)
c) O(n2)
d) O(n log n)
Answer: a
Justification: It refers to n values complexity in the algorithm which can be reduced by choosing the other algorithms.
The complexity of Bubble sort algorithm is
a) O(n)
b) O(log n)
c) O(n2)
d) O(n log n)
Answer: c
Justification: Bubble sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
Question
Randomised algorithms are also called as __________________ and whose behavior is dependent on _______________ in decision making as part of its logic
Answer: probabilistic algorithm and randomness
Question:
Match the following
Answer:
Question:
Match the following
Problem
|
Recurrence Equation
|
||
1
|
Binary Search
|
A
|
tn – tn-1 = 1
|
2
|
Merge Sort
|
B
|
T(n)=2T(n/2)+n-1
|
3
|
Sequential Search
|
C
|
tn = tn-1
+ n
|
4
|
Factorial
|
D
|
T(n)=7T(n/2)+18(n/2)2
|
5
|
Strassen Matrix Multiplication
|
E
|
T(n)=T(n/2)+1
|
6
|
Selection Sort
|
F
|
T(n)=T(n/2)+n-1
|
Answer:
T(n) = Time Complexity
Problem
|
Recurrence
Equation
|
||
1
|
Binary
Search
|
E
|
T(n)=T(n/2)+1
|
2
|
Merge
Sort
|
B
|
T(n)=2T(n/2)+n-1
|
3
|
Sequential
Search
|
A
|
tn
– tn-1 = 1
|
4
|
Factorial
|
F
|
T(n)=T(n/2)+n-1
|
5
|
Strassen
Matrix Multiplication
|
D
|
T(n)=7T(n/2)+18(n/2)2
|
6
|
Selection
Sort
|
C
|
tn
= tn-1 + n
|
Additional
Reference:
|
|||
7
|
Insertion
sort
|
-
|
T(N)
= T(N-1) + N-1
|
8
|
Tree
traversal
|
-
|
T(n)
= 2T(n/2) + 1
|
9
|
Quicksort
|
-
|
(n)
= 2T(n/2) + n
|
10
|
Master
Theorm
|
-
|
T(n)=aT(n/b)+f(n)
|
Tip:
Merge Sort: The merge sort algorithm deals with the problem of sorting a list of n elements. It is able to sort a list of n elements in O(n log n) run-time, which is considerably faster than insertion sort, which takes O(n2).
Merge sort uses a divide and conquer method:
1. If the length of the list is 1, the list is sorted. Return the list.
2. Otherwise, split the list in two (roughly) equal halves and then recursively merge sort the two halves
3. Merge the two sorted halves into one sorted list.