Searching & Sorting GATE CS PYQ Quiz (original) (raw)

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

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?

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?

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?

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

C++ `

#include using namespace std;

int ProcessArray(vector& 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?

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)

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?

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?

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

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

There are 13 questions to complete.

Take a part in the ongoing discussion