You are on page 1of 4

Assignment 9 July 2022 Solution

1. What is the best case complexity of ordered linear search and worst case complexity of
selection sort respectively?
a) O(1), O(n2)
b) O(logn), O(1),
c) O(n), O(logn)
d) O(n2), O(nlogn)

Solution: (a) O(1), O(n2)

2.Which of the following is/are correct?


I. Binary search is applied when elements are sorted.
II. Linear search can be applied when elements are in random order.
III. Binary search can be categorized into divide and conquer rule.

a) I & II
b) Only I
c) I and III
d) I, II & III

Solution: (d) I, II&III

3. What is the recurrence relation for the linear search recursive algorithm?

a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c

Solution: (c) T(n-1)+c

4. Given an array arr = {20, 45, 77, 89, 91, 94, 98,100} and key = 45; what are the mid values
(corresponding array elements) generated in the first and second iterations?
a) 91 and 98
b) 89 and 45
c) 89 and 77
d) 91 and 94

Solution: (b) 89 and 45


In the first iteration: Mid = arr[(0+7)/2] = arr[3] = 89
In the second iteration: Mid = arr[(0+2)/2] = arr[1] = 45
So, the mid values are 89 and 45
5. Consider an array of elements a[7]= {10,4,7,23,67,12,5}, what will be the resultant
array a after third pass of insertion sort.

a) 67, 12, 10, 5, 4, 7


b) 4, 7, 10, 23, 67, 12, 5
c) 4, 5, 7, 67, 10, 12, 23
Assignment 9 July 2022 Solution

d) 10, 7, 4, 67, 23, 12, 5

Solution: (b) 4, 7, 10, 23, 67, 12, 5


Array: 10, 4, 7, 23, 67, 12, 5

After 1st pass: 4, 10, 7, 23, 67, 12, 5 – 4 and 10 got swapped or 4 got inserted at its appropriate
position.

After 2nd pass: 4, 7, 10, 23, 67, 12, 5 – 7 got inserted at its appropriate position.

After 3rd pass: 4, 7, 10, 23, 67, 12, 5 – No change as the value 10 is placed at its appropriate
position already.

6. Select the code snippet which performs unordered linear search iteratively?

a) intunorderedLinearSearch(intarr[], int size, int data)


{
int index;
for(inti = 0; i< size; i++)
{
if(arr[i] == data)
{
index = i;
break;
}
}
return index;
}

b) intunorderedLinearSearch(intarr[], int size, int data)


{
int index;
for(inti = 0; i< size; i++)
{
if(arr[i] == data)
{
break;
}
}
return index;
}
c) intunorderedLinearSearch(intarr[], int size, int data)
{
int index;
for(inti = 0; i<= size; i++)
{
if(arr[i] == data)
{
index = i;
Assignment 9 July 2022 Solution

continue;
}
}
return index;
}

d) None of the above

Solution: (a)
Unordered term refers to the given array, that is, the elements need not be ordered. To search
for an element in such an array, we need to loop through the elements until the desired element
is found.

7. Which of the following input will give worst case time complexity for selection sort to sort
an array in ascending order?
I. 1, 2, 3, 4, 5, 6, 7, 8
II. 8, 7, 6, 5, 4, 3, 2, 1,
III. 8, 7, 5, 6, 3, 2, 1, 4

a) I
b) II
c) II and III
d) I,II and III

Ans : (d)
Selection sort has the same time complexity for best, average and worst cases. Therefore, all
the three arrays will be sorted at the same time complexity of O(n2).

8. Consider the array A[]= {5,4,9,1,3} apply the insertion sort to sort the array . Consider the
cost associated with each sort is 25 rupees, what is the total cost of the insertion sort for sorting
the entire array?
a) 25
b) 50
c) 75
d) 100
Solution: (c)
When the element 1 reaches the first position of the array three comparisons are only required
hence 25 * 3= 75 rupees.

*step 1: 4 5 9 1 3.

*step 2: 1 4 5 9 3

*step 3: 1 3 4 5 9

9. A sorting technique is called stable if:


Assignment 9 July 2022 Solution

a) It takes O(nlog n)time


b) It maintains the relative order of occurrence of non-distinct elements
c) It uses divide and conquer paradigm
d) It takes O(n) space

Solution: (b) A sorting algorithm is called stable if it keeps elements with equal keys in the
same relative order in the output as they were in the input.

10. The average case occurs in the Linear Search Algorithm when
a) The item to be searched is in some where middle of the Array
b) The item to be searched is not in the array
c) The item to be searched is in the last of the array
d) The item to be searched is either in the last or not in the array
Solution: (a)

 The average case occurs in the Linear Search Algorithm when the item to be searched
is in some where middle of the Array.
 The best case occurs in the Linear Search Algorithm when the item to be searched is
in starting of the Array.
 The worst case occurs in the Linear Search Algorithm when the item to be searched is
in end of the Array.
So, option (A) is correct.

You might also like