Sei sulla pagina 1di 4

Questions and Answers

Q.1) What is the number of swaps required to sort n elements using selection sort, in
the worst case?

e.
co
m

A. &#920(n)
B. &#920(n log n)
C. &#920(n2)
D. &#920(n2 log n)

ANSWER : &#920(n)
SOLUTION :
Note that we are concerned about the swaps, not the comparisons. In the best case,
there is no need for any swap. The best case scenario is when the given list of elements
is already in sorted order. In the worst case, we need (n-1) swaps. For example,
consider the list 10, 5, 6, 7, 8, 9. We need 5 swaps. To conclude, 0 <= (the number of
required swaps) < n

in

Q.2) A machine took 200 sec to sort 200 names, using bubble sort. In 800 sec, it can
approximately sort?

tio

nl

A. 400 names
B. 800 names
C. 750 names
D. 800 names

w
.a
p

ANSWER : 400 names


SOLUTION :
For sorting 200 names bubble sort makes 200 x 199/2 = 19900 comparisons. The time
needed for 1 comparison is 200 sec. In 800 sec it can make 80,000 comparisons. We
have to fine n, such that n(n - 1)/2 = 80,000. From this n is approximately 400.
Q.3) A machine needs a minimum of 100 sec to sort 1000 names by quick sort.The
minimum time needed to sort 100 names will be approximately ?

A. 50.2 sec
B. 6.7 sec
C. 72.7 sec
D. 11.2 sec

ANSWER : 6.7 sec


SOLUTION :
In the best case quick sort algorithm makes n log(n) comparisons. so 1000 x log (1000)
= 9000 comparisons, which takes 100 sec. To sort 100 names a minimum of 100
log(100) = 600 comparisons are needed. This takes 100 x 600/9000 = 6.7 sec.
Q.4) Which of following algorithm scans the list by swapping the entries whenever pair
of adjacent keys are out of desired order?
A. Insertion sort
B. Quick sort
C. Shell sort
D. Bubble sort
ANSWER : Bubble sort
SOLUTION :
Bubble sort only is the algorithm from the given options which compares the adjacent
keys.
Q.5) For the quick sort algorithm, what is the time complexity of the best/worst case?
A. best case: O(n)
worst case: O(n*n)
B. best case: O(n)
worst case: O(n*log(n))
C. best case: O(n*log(n)) worst case: O(n*log(n))

Page 1

Questions and Answers


D. best case: O(n*log(n)) worst case: O(n*n)

e.
co
m

ANSWER : best case: O(n*log(n)) worst case: O(n*n)


SOLUTION :
The worst case here is, if the list is already sorted in descending order and we want to
sort it in ascending order. In this case it takes O(n2) time. Otherwise it takes O( n *
log(n) ) time.
Q.6) To sort many large object or structures, it would be most efficient to
A. Place reference to them in and array an sort the array
B. Place them in a linked list and sort the linked list
C. Place pointers to them in an array and sort the array
D. Place them in an array and sort the array

nl

in

ANSWER : Place them in a linked list and sort the linked list
SOLUTION :
Dynamic structure (Memory Allocated at run-time).
We can have more than one datatype.
Re-arrange of linked list is easy (Insertion-Deletion).
It doesn^aEURTMt waste memory.

w
.a
p

A. mnp
B. mp
C. mn
D. np

tio

Q.7) The complexity of multiplying two matrices of order m*n and n*p is

ANSWER : mnp
SOLUTION :
The complexity of multiplying two matrices of order m*n and n*p is mnp
Q.8) Which of the following is not a limitation of binary search algorithm?

A. must use a sorted array


B. requirement of sorted array is expensive when a lot of insertion and deletions are
needed
C. there must be a mechanism to access middle element directly
D. binary search algorithm is not efficient when the data elements are more than 1000.
ANSWER : binary search algorithm is not efficient when the data elements are more
than 1000.
SOLUTION :
binary search algorithm is not efficient when the data elements are more than 1000.We
can search more than 1000 element in binary search there is no limitation.
Q.9) The way a card game player arranges his cards as he picks them up one by one,
is an example of ?
A. bubble sort
B. selection sort
C. insertion sort
D. merge sort
ANSWER : insertion sort
SOLUTION :
He scans throught the rest of the cards and pick the one with least value and places it
next to the point till which he has already sorted the cards

Page 2

Questions and Answers


Q.10) Binary search algorithm cannot be applied to

e.
co
m

A. sorted linked list


B. sorted binary trees
C. sorted linear array
D. pointer array
ANSWER : sorted linked list
SOLUTION :
The binary search algorithm depends on equal (or direct) access time for any selected
element of a list. A linked list, however, does not provide that, so a binary search is
inappropriate for a linked list.
Q.11) The average search time of hashing, with linear probing will be less if the load
factor

nl

in

A. Is far less than one


B. equals one
C. is far greater than one
D. none of these

tio

ANSWER : Is far less than one


SOLUTION :
Load factor is the ratio of number of records that are currently present and the total
number of records that can be present. If the load factor is less, free space will be more.
This means probability of collision is less. So, search time will be less.

w
.a
p

Q.12) If a node having two children is deleted from a binary tree, it is replaced by its
A. Inorder predecessor
B. Inorder successor
C. Preorder predecessor
D. None of the above

ANSWER : Inorder successor


SOLUTION :
In Binary Tree, Inorder successor of a node is the next node in Inorder traversal of the
Binary Tree. Inorder Successor is NULL for the last node in Inoorder traversal.
In Binary Search Tree, Inorder Successor of an input node can also be defined as the
node with the smallest key greater than the key of input node. So, it is sometimes
important to find next node in sorted order.
Q.13) The searching technique that takes O (1) time to find a data is
A. Linear Search
B. Binary Search
C. Hashing
D. Tree Search
ANSWER : Hashing
SOLUTION :
Hash tables are often used to implement associative arrays, sets and caches. Like
arrays, hash tables provide constant-time O(1) lookup on average, regardless of the
number of items in the table. However, the rare worst-case lookup time can be as bad
as O(n). Compared to other associative array data structures, hash tables are most
useful when large numbers of records of data are to be stored.
Q.14) For the bubble sort algorithm, what is the time complexity of the best/worst case?
(assume that the computation stops as soon as no more swaps in one pass)
A. best case: O(n)
B. best case: O(n)

worst case: O(n*n)


worst case: O(n*log(n))

Page 3

Questions and Answers


C. best case: O(n*log(n)) worst case: O(n*log(n))
D. best case: O(n*log(n)) worst case: O(n*n)

e.
co
m

ANSWER : best case: O(n)


worst case: O(n*n)
SOLUTION :
Best Case is the one in which it is already sorted, and hence it takes O(n) time,
otherwise it takes O(n2) time.
Q.15) Which of the following is not the required condition for binary search algorithm?
A. The list must be sorted
B. The list must be sorted
C. There must be mechanism to delete and/or insert elements in list
D. none of above

nl

in

ANSWER : There must be mechanism to delete and/or insert elements in list


SOLUTION :
There is no such require condition in binary search algorithm.
It^aEURTMs not necessary that you^aEURTMve to add or delete any element in list.

w
.a
p

A. O(1)
B. O(log n)
C. O(n log n)
D. O(n).

tio

Q.16) The time taken by nondeterministic sorting algorithm is ______

ANSWER : O(n).
SOLUTION :
As there it sorts the entries in one loop. Because in nondeterministic sorting algorithms
the abstractions are used which can^aEURTMt be programmed.

Q.17) 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 : O (n2/2)
SOLUTION :
Each partition gives unbalanced
split. We get T(n) = T(n &Acirc;&iexcl; 1) + (n) = &#920(n2).
In worst case, Quick Sort as bad as BubbleSort.
The worst-case occurs when the list is already
sorted, and the last element chosen as pivot.

Page 4

Potrebbero piacerti anche