True or False: Stack
True or False: Stack
True or False: Stack
d) Which data structure is used for implementing a FIFO branch and bound strategy?
A : Stack
B : Queue
C : Array
D : Linked List
g) ___ is the maximum amount of time an algorithm takes to execute a specific set of inputs.
a. Running time
b. Average case time complexity
c. Worst case time complexity
d. Best case time complexity
Q 3:
1. Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero
integers m and n. write an Euclid’s algorithm (in steps)
2. Design an algorithm to find all the common elements in two sorted lists of numbers. For
example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2, 5, 5. What is the
maximum number of comparisons your algorithm makes if the lengths of the two given
lists are m and n respectively?
(answ) :
Initialize the list of common elements to empty.
Starting with the first elements of the lists given, repeat the following until one of the lists
becomes empty.
Compare the current elements of the two lists: if they are equal, add this element to the list of
common elements and move to the next elements of both lists (if any); otherwise, move to the
element following the smaller of the two involved in the comparison.
The maximum number of comparisons, which is made by this algorithm on some lists with no
common elements such as the first m positive odd numbers and the first n positive even numbers,
is equal to m + n – 1
Q 4:
What is time complexity of fun()?
int fun(int n)
{
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}
(A) O(n^2)
(B) O(nLogn)
(C) O(n)
(D) O(nLognLogn)
Explanation: For a input integer n, the innermost statement of fun() is executed
following times.
n + n/2 + n/4 + … 1
So time complexity T(n) can be written as
T(n) = O(n + n/2 + n/4 + … 1) = O(n)
The value of count is also n + n/2 + n/4 + .. + 1
int fun(int n)
{
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i; j > 0; j--)
count = count + 1;
return count;
}
Question :
1) Two main measures for the efficiency of an algorithm are ……………., ……………….
2) List the most important problem types in design and analysis of algorithm
1) …………………………………..
2) ……………………………………
3) …………………………………...
4) ……………………………………
5) ……………………………………
6) ……………………………………
1) sorting
2) searching
3) string processing
4) graph problems
5) combinatorial problems
6) geometric problems
7) numerical problems
3) The running time T(n) of a program implementing this algorithm on that computer by
the formula
T (n) ≈ cop C(n).
Where cop is ............ execution time for basic operation.......................
and C(n) is .................. Number of times basic operation is executed ........................
4- Suppose we want to determine the efficiency of the algorithm, then how we can measure
the space factor.
a. To count the maximum memory required by the algorithm
b. To count the minimum memory required by the algorithm
c. To count the average memory required by the algorithm
d. To count the maximum disk space needed by the algorithm
ALGORITHM Mystery(n)
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
S←S+i∗i
return S
However, the order in which the product is parenthesized affects the number of simple arithmetic
operations needed to compute the product. For example, if A is a 10 × 30 matrix, B is a 30 × 5
matrix, and C is a 5 × 60 matrix, then computing (AB)C needs (10×30×5) + (10×5×60) = 1500 +
3000 = 4500 operations while computing A(BC) needs (30×5×60) + (10×30×60) = 9000 + 18000
= 27000 operations. Clearly, the first method is more efficient.
4- Fined the Input size and basic operation of the following examples
Problem Input size measure Basic operation
Find x in an array
Traverse a tree
Decrease-and-Conquer
Insertion Sort
Answer: b
Explanation: An insertion algorithm consists of N-1 passes when an array of N elements is
given.
Answer: d
Explanation: The average case analysis of a tight bound algorithm is mathematically achieved
to be O(N2).
4. Any algorithm that sorts by exchanging adjacent elements require O(N2) on average.
a) True
b) False
Answer: a
Explanation: Each swap removes only one inversion, so O(N2) swaps are required.
Answer: a
Explanation: The total number of pairs in a list L is N(N-1)/2. Thus, an average list has half this
amount, or N(N-1)/4 inversions.
6. 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)
Answer: c
Explanation: If the input is pre-sorted, the running time is O(N), because the test in the inner
for loop always fails immediately and the algorithm will run quickly.
7. What will be the number of passes to sort the elements using insertion sort?
14, 12,16, 6, 3, 10
a) 6
b) 5
c) 7
d) 1
Answer: b
Explanation: The number of passes is given by N-1. Here, N=6. Therefore,
6-1=5 passes.
8. For the following question, how will the array elements look like after second pass?
34, 8, 64, 51, 32, 21
a) 8, 21, 32, 34, 51, 64
b) 8, 32, 34, 51, 64, 21
c) 8, 34, 51, 64, 32, 21
d) 8, 34, 64, 51, 32, 21
Answer: d
Explanation: After swapping elements in the second pass, the array will look like, 8, 34, 64, 51,
32, 21.
Answer: a
Explanation: Arranging a pack of cards mimics an insertion sort. Database scenario is an
example for merge sort, arranging books is a stack and real-time systems uses quick sort.
10. In C, what are the basic loops required to perform an insertion sort?
a) do- while
b) if else
c) for and while
d) for and if
Answer: c
Explanation: To perform an insertion sort, we use two basic loops- an outer for loop and an
inner while loop.
11. Binary search can be used in an insertion sort algorithm to reduce the number of
comparisons.
a) True
b) False
Answer: a
Explanation: Binary search can be used in an insertion sort algorithm to reduce the number of
comparisons. This is called a Binary insertion sort.
12. Which of the following options contain the correct feature of an insertion sort algorithm?
a) anti-adaptive
b) dependable
c) stable, not in-place
d) stable, adaptive
Answer: d
Explanation: An insertion sort is stable, adaptive, in-place and incremental in nature.
13. Which of the following sorting algorithms is the fastest for sorting small arrays?
a) Quick sort
b) Insertion sort
c) Shell sort
d) Heap sort
Answer: b
Explanation: For sorting small arrays, insertion sort runs even faster than quick sort. But, it is
impractical to sort large arrays.
14. 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
Answer: a
Explanation: The best case input for an insertion sort algorithm runs in linear time and is given
by O(N).
15. 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
Answer: bExplanation: The worst case input for an insertion sort algorithm will be an array
sorted in reverse order and its running time is quadratic.
-----------------------------------------------------------
1. Consider the following lists of partially sorted numbers. The double bars (||) represent the
sort of marker. How many comparisons and swaps are needed to sort the next number.
[1 3 4 8 9 || 5 2]
A. 2 comparisons, 3 swaps
B. 3 comparisons, 2 swaps
C. 4 comparisons, 3 swaps
D. 3 comparisons, 4 swaps
2. Consider the following lists of partially sorted numbers. The double bars represent the sort
marker. How many comparisons and swaps are needed to sort the next number. [1 3 4 5 8 9 ||
2]
A. 5 comparisons, 4 swaps
B. 4 comparisons, 5 swaps
C. 6 comparisons, 5 swaps
D. 5 comparisons, 6 swaps
3. If all the elements in an input array is equal for example {1,1,1,1,1,1}, What would be the
running time of the Insertion Algorithm?
A. O(2N)
B. O(n^2)
C. O(n)
4. What operation does the Insertion Sort use to move numbers from the unsorted section to
the sorted section of the list?
B. Swapping
5. What are the correct intermediate steps of the following data set when it is being sorted
with the Insertion sort? 15,20,10,18
6. What will be the number of passes to sort the elements using insertion sort? 14, 12,16, 6, 3,
10
A. 7
B. 8
C. 1
D. 5
7. Which of the following options contain the correct feature of an insertion sort algorithm?
A. Dependable
B. Stable, adaptive
C. Anti-stable
A. True
B. False
A. Merge sort
B. Quick sort
C. Insertion sort
10. Which of these sorting algorithms is the fastest for sorting small arrays?
A. Insertion sort
B. Quick sort
---------
Answer: a
Explanation: During insertion sort, the relative order of elements is not changed. Therefore, it is
a stable sorting algorithm. And insertion sort requires only O(1) of additional memory space.
Therefore, it sorts In-place.
2. Which of the following sorting algorithm is best suited if the elements are already sorted?
a) Heap Sort
b) Quick Sort
c) Insertion Sort
d) Merge Sort
View Answer
Answer: c
Explanation: The best case running time of the insertion sort is O(n). The best case occurs when
the input array is already sorted. As the elements are already sorted, only one comparison is
made on each pass, so that the time required is O(n).
3. The worst case time complexity of insertion sort is O(n2). What will be the worst case time
complexity of insertion sort if the correct position for inserting element is calculated using
binary search?
a) O(nlogn)
b) O(n2)
c) O(n)
d) O(logn)
Answer: b
Explanation: The use of binary search reduces the time of finding the correct position from
O(n) to O(logn). But the worst case of insertion sort remains O(n2) because of the series of
swapping operations required for each insertion.
Answer: a
Explanation: In the incremental algorithms, the complicated structure on n items is built by first
building it on n − 1 items. And then we make the necessary changes to fix things in adding the
last item. Insertion sort builds the sorted sequence one element at a time. Therefore, it is an
example of an incremental algorithm.
Answer: b
Explanation: In insertion sort, the element is A[j] is inserted into the correct position in the
sorted sequence A[1… j – 1]. So, condition given in (j > 0) && (arr[j − 1] > value) will implement
while loop correctly.
6. Which of the following is good for sorting arrays having less than 100 elements?
a) Quick Sort
b) Selection Sort
c) Merge Sort
d) Insertion Sort
Answer: d
Explanation: The insertion sort is good for sorting small arrays. It sorts smaller arrays faster
than any other sorting algorithm.
7. Consider an array of length 5, arr[5] = {9,7,4,2,1}. What are the steps of insertions done while
running insertion sort on the array?
a) 7 9 4 2 1 4 7 9 2 1 2 4 7 9 1 1 2 4 7 9
b) 9 7 4 1 2 9 7 1 2 4 9 1 2 4 7 1 2 4 7 9
c) 7 4 2 1 9 4 2 1 9 7 2 1 9 7 4 1 9 7 4 2
d) 7 9 4 2 1 2 4 7 9 1 4 7 9 2 1 1 2 4 7 9
Answer: a
Explanation: The steps performed while running insertion sort on given array are:
Initial : 9 7 4 2 1 key = 7
7 9 4 2 1 key = 4
4 7 9 2 1 key = 2
2 4 7 9 1 key = 1
12479
In each step, the key is the element that is compared with the elements present at the left side
to it.
8. Statement 1: In insertion sort, after m passes through the array, the first m elements are in
sorted order.
Statement 2: And these elements are the m smallest elements in the array.
a) Both the statements are true
b) Statement 1 is true but statement 2 is false
c) Statement 1 is false but statement 2 is true
d) Both the statements are false
Answer: b
Explanation: In insertion sort, after m passes through the array, the first m elements are in
sorted order but they are whatever the first m elements were in the unsorted array.
9. In insertion sort, the average number of comparisons required to place the 7th element into
its correct position is ____
a) 9
b) 4
c) 7
d) 14
Answer: b
Explanation: On average (k + 1) / 2 comparisons are required to place the kth element into its
correct position. Therefore, average number of comparisons required for 7th element = (7 +
1)/2 = 4.
10. Which of the following is not an exchange sort?
a) Bubble Sort
b) Quick Sort
c) Partition-exchange Sort
d) Insertion Sort
Answer: d
Explanation: In Exchange sorts, we compare each element of an array and swap those
elements that are not in their proper position. Bubble Sort and Quick Sort are exchange sorts.
Quick Sort is also called as Partition-exchange Sort. Insertion sort is not an exchange
----------------
==================================================
Q.
Which of these are not types of decrease and conquer
1) decrease by a constant
2) decrease by one
3) decrease by a constant factor
4) variable size decrease
Q.
binary search is the example of...
1) decrease by one example
2) decrease by a constant example
3) decrease by a constant factor example
Q.
topological sort is (select which is wrong)
1) example of decrease by a constant
2) variable size decrease
3) working only if graph is a dag
Q.
sample of variable size decrease is not
1) euclids
2) lomuto partitioning
3) median search
4) source removal
2.