Searching and Sorting · Algorithms · GATE CSE
Marks 1
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
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
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 respectivelyA 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?
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?
(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