Searching & Sorting GATE CS PYQ Quiz

Last Updated :
Discuss
Comments

Question 1

You have an array of n elements. Suppose you implement quick sort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is

  • O(n2)

  • O(n*log(n))

  • Theta(n*log(n))

  • O(n3)

Question 2

Randomized quicksort is an extension of quicksort where the pivot is chosen randomly. What is the worst case complexity of sorting n numbers using randomized quicksort?

  • O(n)

  • O(n*log(n))

  • O(n2)

  • O(n!)

Question 3

Let P be a QuickSort Program to sort numbers in ascending order using the first element as pivot. Let t1 and t2 be the number of comparisons made by P for the inputs {1, 2, 3, 4, 5} and {4, 1, 5, 3, 2} respectively. Which one of the following holds?

  • t1 = 5

  • t1 < t2

  • t1 > t2

  • t1 = t2

Question 4

Consider the following array.


Which algorithm out of the following options uses the least number of comparisons (among the array elements) to sort the above array in ascending order?

  • Selection sort

  • Mergesort

  • Insertion sort

  • Quicksort using the last element as pivot

Question 5

Consider the function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order. 

C++
#include <vector>
using namespace std;

int ProcessArray(vector<int>& listA, int x, int n) {
    int i = 0;
    int j = n - 1;
    while (i <= j) {
        int k = (i + j) / 2;
        if (x <= listA[k]) {
            j = k - 1;
        }
        if (listA[k] <= x) {
            i = k + 1;
        }
    }
    if (listA[k] == x) {
        return k;
    } else {
        return -1;
    }
}
C
#include <stdio.h>

int ProcessArray(int *listA, int x, int n)
{
    int i, j, k;
    i = 0;
    j = n-1;
    do
    {
        k = (i+j)/2;
        if (x <= listA[k])
            j = k-1;
        if (listA[k] <= x)
            i = k+1;
    }
    while (i <= j);
    if (listA[k] == x)
        return(k);
    else
        return -1;
}
Java
public class Main {
    public static int ProcessArray(int[] listA, int x, int n) {
        int i = 0, j = n - 1, k;
        do {
            k = (i + j) / 2;
            if (x <= listA[k])
                j = k - 1;
            if (listA[k] <= x)
                i = k + 1;
        } while (i <= j);
        if (listA[k] == x)
            return k;
        else
            return -1;
    }
}
Python
def ProcessArray(listA, x, n):
    i = 0
    j = n - 1
    while i <= j:
        k = (i + j) // 2
        if x <= listA[k]:
            j = k - 1
        if listA[k] <= x:
            i = k + 1
    if listA[k] == x:
        return k
    else:
        return -1
JavaScript
function ProcessArray(listA, x, n) {
    let i = 0;
    let j = n - 1;
    let k;
    do {
        k = Math.floor((i + j) / 2);
        if (x <= listA[k])
            j = k - 1;
        if (listA[k] <= x)
            i = k + 1;
    } while (i <= j);
    if (listA[k] === x)
        return k;
    else
        return -1;
}

Which one of the following statements about the function ProcessArray is CORRECT?

  • It will run into an infinite loop when x is not in listA.

  • It is an implementation of binary search.

  • It will always find the maximum element in listA.

  • It will return −1 even when x is present in listA.

Question 6

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

(Α) Θ(n)

(B) Ө(n log n)

(C) Ө(n²)

(D) Ө(n² log n)

  • A

  • B

  • C

  • D

Question 7

Let A be a sequence of 8 distinct integers sorted in ascending order. How many distinct pairs of sequences, B and C are there such that (i) each is sorted in ascending order, (ii) B has 5 and C has 3 elements, and (iii) the result of merging B and C gives A?
 

  • 256
     

  • 56
     

  • 30
     

  • 2
     

Question 8

Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous.

C
1.   f(int Y[10], int x) {
2.     int i, j, k;
3.     i = 0; j = 9;
4.     do {
5.             k =  (i + j) /2;
6.             if( Y[k] < x)  i = k; else j = k;
7.         } while(Y[k] != x && i < j);
8.     if(Y[k] == x) printf ("x is in the array ") ;
9.     else printf (" x is not in the array ") ;
10. }

On which of the following contents of Y and x does the program fail?

  • Y is [1 2 3 4 5 6 7 8 9 10] and x < 10

  • Y is [1 3 5 7 9 11 13 15 17 19] and x < 1

  • Y is [2 2 2 2 2 2 2 2 2 2] and x > 2

  • Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is even

Question 9

Consider the data given in above question, the correction needed in the program to make it work properly is

  • Change line 6 to: if (Y[k] < x) i = k + 1; else j = k-1;

  • Change line 6 to: if (Y[k] < x) i = k - 1; else j = k+1;

  • Change line 6 to: if (Y[k] <= x) i = k; else j = k;

  • Change line 7 to: } while ((Y[k] == x) && (i < j));

Question 10

Which one of the following in place sorting algorithms needs the minimum number of swaps?

  • Quick sort

  • Insertion sort

  • Selection sort

  • Heap sort

There are 13 questions to complete.

Take a part in the ongoing discussion