edited by
16,413 views
55 votes
55 votes

Suppose you are provided with the following function declaration in the C programming language.

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 $k^{th}$ smallest element in an array $a[\:]$ of size $n$ using the partition function. We assume $k \leq 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

  1. $(a,$ left_end$, k) $ and $ (a+$left_end$+1, n-$left_end$-1, k-$left_end$-1)$
  2. $(a,$ left_end$, k) $ and $ (a, n-$left_end$-1, k-$left_end$-1)$
  3. $(a+$ left_end$+1, n-$left_end$-1, k-$left_end$-1) $ and $(a, $ left_end$, k)$
  4. $(a, n-$left_end$-1, k-$left_end$-1)$ and $(a, $left_end$, k)$
edited by

7 Answers

0 votes
0 votes

Consider an array A:

n=5

 3  6  2  1  4

We apply partition function as partition(A, 5).​​​​​​

'3' is the pivot element.

So after partition function, we have:

 2  1  3  6  4

So, Left_End=2 i.e. number of element on the left side of '3'. Exclude 3 from the count.

Now suppose k=2

Left_End+1=3 ( !=2)

Now, k<Left_End+1

Now, 2nd smallest element should be the the 2nd element of the left subarray of 3 if it was sorted.

So 'k' remains 2, size of the subarray=2 and array starts from A.

So first statement will be:

(A,2,2): (a, Left_End, k)

Now, if k=4

Left_End+=3 (!=4)

Now, k>Left_End+1

So, 4th smallest element would be the first element of right subarray if it was sorted.

So 'k' becomes 1, size of the array=2 and we start from index=3 ( a+3 )

So second statement will be

(A+3, 2, 1): ( a+left_end+1, n-(left_end+1), k-(left_end+1))

Hence, option A would be thr right answer.

​​​

0 votes
0 votes
QuickSort is used as a sorting algorithm.In QuickSort, we pick a pivot element, then move the pivot element to its correct position and partition the array around it. The idea is, not to do complete quicksort, but stop at the point where pivot itself is k’th smallest element. Also, not to recur for both left and right sides of pivot, but recur for one of them according to the position of pivot.
0 votes
0 votes
I think that the question is wrong because if partition returns no. of elements in the left part, then the algorithm is not correct (from the first condition). It is scary that so many wrong questions have been given in GATE.
Answer:

Related questions

62 votes
62 votes
12 answers
1
go_editor asked Feb 12, 2015
20,905 views
Consider the following C function.int fun(int n) { int x=1, k; if (n==1) return x; for (k=1; k<n; ++k) x = x + fun(k) * fun (n-k); return x; }The return value of $fun(5)$...
25 votes
25 votes
3 answers
2
go_editor asked Feb 12, 2015
6,928 views
Given below are some algorithms, and some algorithm design paradigms.$$\begin{array}{ll|ll}\hline \text{1.} & \text{Dijkstra's Shortest Path} & \text{i.} & \text{Divide a...
61 votes
61 votes
6 answers
3
go_editor asked Feb 12, 2015
17,405 views
An unordered list contains $n$ distinct elements. The number of comparisons to find an element in this list that is neither maximum nor minimum is$\Theta(n \log n)$$\Thet...
88 votes
88 votes
5 answers
4