Sunteți pe pagina 1din 4

Quiz-3 Time-1hr

Algorithms & Programming

Q1.Which of the following is/are true about the following sorting algorithms

a)Insertion an element in to min heap or max heap which already contains ‘n’
elements required O(logn)times in worst case.

b)If the array is already sorted or almost sorted in the insertion sort will give best
case of O(n).But quick sort will give worst case of O(n^2).

c)If the array size is very small then insertion sort is preferable but Merge sort is
not.

d)Bubble sort Contain (n-1)passes and every pass contain max n comparisons so it
will have O(n^2) in all Best ,average and worst case.

e)None.

Q2. Suppose we are sorting an array of eight integers using quicksort, and we have
just finished the first partitioning with the array looking like this: 2, 5, 1, 7, 9, 12, 11,
10 Which statement is correct?

a)The pivot could be either 7 or 9

b)The pivot could be 7 but no 9

c)The pivot is not 7,but it could be 9

d)Neither 7 nor 9.

Q3(a)What are the correct intermediate steps of the following data set when it is
being sorted with the Selection sort? Given:15,20,10,18

a)10, 20,15,18 -- 10,15,20,18 – 10,15,18,20

b)15,20,10,18 -- 15,10,20,18 -- 10,15,20,18 – 10,15,18,20

c)15,18,10,20 -- 10,18,15,20 -- 10,15,18,20 – 10,15,18,20

d)15,10,20,18 -- 15,10,18,20 – 10,15,18,20


Q3(b)A selection sort compares adjacent elements, and swaps them if they are in
the wrong order.

a)True

b)False
c)Depends on Elements
d)None of the above

Q4.Which sorting algorithm will take least time when all elements of input array are
identical? Consider typical implementations of sorting algorithms.

a)Insertion Sort

b)Heap Sort

c)Merge Sort

d)Selection Sort

Q5. Which of the following is true about merge sort?

a)Merge sort is based on Divide and conquer method. It takes the list to be
sorted and divide it in half to create two unsorted lists.

b)Merge sort is stable sort by nature

c)Merge sort outperforms heap-sort in most practical situations.

d)All of the above.

Q6.The worst case running times of Insertion sort, Merge sort and Quick sort,
respectively, are:

a)O(n log n),O(n log n) and O(n^2)

b)O(n^2),O(n^2),O(nlog n)

c)O(n^2),O(n log n),O(n logn)

d)O(n^2),O(n logn) ,O(n^2)


Q7. Assume that we use Bubble Sort to sort n distinct elements in ascending order.
When does the best case of Bubble Sort occur?

a)When elements are sorted in ascending order

b)When elements are sorted in desending order

c)When elements are not sorted by any order

d)There is no best case for bubble sort it always take O(n*n) time.

Q8.What are the correct intermediate steps of the following data set when it
is being sorted with the Insertion sort? 15,20,10,18

a)15,20,10,18 -- 10,15,20,18 -- 10,15,18,20 -- 10,15,18,20

b)15,18,10,20 -- 10,18,15,20 -- 10,15,18,20 -- 10,15,18,20

c)15,10,20,18 -- 15,10,18,20 -- 10,15,18,20

d)10, 20,15,18 -- 10,15,20,18 -- 10,15,18,20

Q9(a) Consider the following C program segment.

# include <stdio.h>
int main( )
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = '0' ;
printf ("%s", s1);
}

What will be printed by the program?

a)12

b)120400

c)1204

d)1034
Q9(b) What will be output of the program if characters ‘a’,’b’and ‘c’ enter as input

#include<stdio.h>

void fun(); /*function prototype */

int main() {

fun();

printf(“\n”); return 0;}

void fun() {

char c;

if((c= getchar())!=’\n’)

fun();

printf(“%c”,c);}

Q10(a) Given the following elements in an array, construct a min-


heap( either by repetitive insertion or by linear time algorithm), and show
the final result in the array.

97,25,15,42,16,5,7,0,8,66,21,3,12,40.

b)What will be the tree after deletion of root node and insertion of node 56.
First deletion happens then insertion build the min heap after performing
these operations.

S-ar putea să vă placă și