Searching and Sorting · Algorithms · GATE CSE
Marks 1
Consider an array $A=[10,7,8,19,41,35,25,31]$. Suppose the merge sort algorithm is executed on array $A$ to sort it in increasing order. The merge sort algorithm will carry out a total of 7 merge operations.
A merge operation on sorted left array $L$ and sorted right array $R$ is said to be void if the output of the merge operation is the elements of array $L$ followed by the elements of array $R$.
The number of void merge operations among these 7 merge operations is $\_\_\_\_$ . (answer in integer)
$$\text { The pseudocode of a function fun( ) is given below : }$$
fun(int A[0, .., n-1]) {
for i = 0 to n-2
for j=0 to n-i-2
if (A[]]>A[j + 1])
then swap A[j] and A[j+1]
}
Let $A[0, \ldots, 29]$ be an array storing 30 distinct integers in descending order. The number of swap operations that will be performed, if the function fun( ) is called with $A[0, \ldots, 29]$ as argument, is _________. (Answer in integer)
Let $A$ be an array containing integer values. The distance of $A$ is defined as the minimum number of elements in $A$ that must be replaced with another integer so that the resulting array is sorted in non-decreasing order. The distance of the array [2, 5, 3, 1, 4, 2, 6] is __________
Consider the following array.
|
23 |
32 |
45 |
69 |
72 |
73 |
89 |
97 |
Which algorithm out of the following options uses the least number of comparisons (among the array elements) to sort above array in ascending order?
$$\,\,\,\,\,\,\,{\rm I}.\,\,\,\,\,\,\,$$ Quicksort runs in $$\Theta \left( {{n^2}} \right)$$ time
$$\,\,\,\,\,{\rm I}{\rm I}.\,\,\,\,\,\,\,$$ Bubblesort runs in $$\Theta \left( {{n^2}} \right)$$ time
$$\,\,\,{\rm I}{\rm I}{\rm I}.\,\,\,\,\,\,\,$$ Mergesort runs in $$\Theta \left( n \right)$$ time
$$\,\,\,{\rm I}V.\,\,\,\,\,\,\,$$ Insertion sort runs in $$\Theta \left( n \right)$$ time
$$\,\,\,\,\,\,\,\,$$$$〈89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100〉$$
The minimum number of interchanges needed to convert it into a max-heap is
Give the correct matching for the following pairs:
Group - 1
(A) $${\rm O}(\log n)$$(B) $${\rm O}(n)$$
(C) $${\rm O}(n\log n)$$
(D) $${\rm O}({n^2})$$
Group - 2
(P) Selection(Q) Insertion sort
(R) Binary search
(S) Merge sort
| A. All pairs shortest path | 1. Greedy |
| B. Quick Sort | 2. Depth-First Search |
| C. Minimum weight spanning tree | 3. Dynamic Programming |
| D. Connected Components | 4. Divide and Conquer |
Marks 2
Consider an array $A$ of integers of size $n$. The indices of $A$ run from 1 to $n$. An algorithm is to be designed to check whether $A$ satisfies the condition given below.
$\forall i, j \in\{1, \ldots, n-1\}$ such that $i>j,(A[i+1]-A[i])>(A[j+1]-A[j])$
Which one of the following gives the worst case time complexity of the fastest algorithm that can be designed for the problem?
An array $A$ of length $n$ with distinct elements is said to be bitonic if there is an index $1=i=n$ such that $A[1 . . i]$ is sorted in the non-decreasing order and $A[i+1 . . n]$ is sorted in the non-increasing order.
Which ONE of the following represents the best possible asymptotic bound for the worstcase number of comparisons by an algorithm that searches for an element in a bitonic array $A$?
int partition(int a[], int n);
The function treats the first element of a[ ] as a pivot and rearranges the array so that all elements less than or equal to the pivot is in the left part of the array, and all elements greater than the pivot is in the right part. In addition, it moves the pivot so that the pivot is the last element of the left part. The return value is the number of elements in the left part.
The following partially given function in the C programming language is used to find the kth smallest element in an array a[ ] of size n using the partition function. We assume k≤n.
int kth_smallest (int a[], int n, int k)
{
int left_end = partition (a, n);
if (left_end+1==k) {
return a[left_end];
}
if (left_end+1 > k) {
return kth_smallest (___________);
} else {
return kth_smallest (___________);
}
}
The missing arguments lists are respectively| Array Index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| Value | 40 | 30 | 20 | 10 | 15 | 16 | 17 | 8 | 4 |
Now consider that a value 35 is inserted into this heap. After insertion, the new heap is
A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4] location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property.
Which one of the following is a valid sequence of elements in an array representing 3-ary max heap?
A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4] location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property.
Suppose the elements 7, 2, 10 and 4 are inserted, in that order, into the valid 3- ary max heap found in the previous question. Which one of the following is the sequence of items in the array representing the resultant heap?
(Hint : Use a heap data structure)
List - I
(M) Tn = Tn - 1 + n(N) Tn = Tn/2 + n
(O) Tn = Tn/2 + nlog n
(P) Tn = Tn - 1 + log n
List - II
(U) Tn= O(n)(V) Tn = O(nlogn)
(W) Tn = O(n2)
(X) Tn = O(log2n)
89, 19, 40, 17, 12, 10, 2, 5, 7, 11, 6, 9, 70
into a heap with the maximum element at the root isList - I
(a) Heap construction(b) Constructing hash table witn linear probing
(c) AVL Tree construction
(d) Digital tree construction
List - II
(p) $$\Omega \left( {n\log _{10}^n} \right)$$(q) O(n)
(r) O(n2)
(s) $$\Omega \left( {n\log _2^n} \right)$$
List - I
(a) Straseen's matrix multiplication algorithm(b) Kruskal's minimum spanning tree algorithm
(c) Bioconnected components algorithm
(d) Floyd's shortest path algorithm
List - II
(p) Greedy method(q) Dynamic programming
(r) Divide and Conquer
(s) Depth first search