edited by
27,852 views
78 78 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)$

8 Answers

Best answer
58 58 votes

We have to find the $k^{th}$ smallest element.

if (left_end+1 > k) 

If the above condition is true, it means we have $k^{th}$ smallest element on the left array, whose size is left_end instead of $n$ for the original array. (The "+1" is used because array index in C language starts from 0). So, we can do a recursive call as 

  • kth_smallest( a, left_end, k); 

If the above condition is false, and left_end $+1\neq k,$ it means $k^{th}$ smallest element is on the right part of the array and it will be $(k - \text{left_end}-1)^{th}$ element there as left_end+1 elements are gone in the left part. So, the recursive call will be 

  • kth_smallest( a + left_end + 1, n - left_end – 1, k - left_end - 1); 

Correct Option: A.

edited by
67 67 votes

First of all, here the return value is the number of elements less than the pivot

Pivot is just to minimize searching

So, now we are assuming our array has $10$ elements, $N=10 , k=8$

STEP 1:          After Partition()

  • left_end = 4
  • left_end+1 < k
  • so, (a+left_end+1, n-left_end-1, k-left_end-1)
  • (a+5, 5, 3)

STEP 2: After  Partition()

  • left_end= 1
  • left_end+1 < k
  • so, (a+2, 3, 1)

STEP 3: After Partition()

  • left_end = 2
  • left_end +1 > k
  • so, (a, 2, 1)

STEP 4: After Partition()

  • left_end = 1
  • left_end +1 > k
  • so, (a,1,1)

STEP 5:

  • left_end = 0
  • left_end+1 = =k
  • a[left_end] = 8

So, in STEP 1 and STEP 2 'else' condition is satisfied, and STEP 3 and STEP 4 'if ' condition is satisfied.

Here, partition is called and it returns the left_end value

Answer will be (A).

edited by
8 8 votes
Ans A.
3 3 votes

Dry run it.

If the "if" condition is true, required element is in the left part of the array. So, we need to pass just the left part of the array, as the whole array.

If the "else" condition is true, required element is in the right part of the array. So, we need to pass just the right part of the array, as the whole array.

So, option A

 

Also, adding from Manu Thakur's comment — in the else part, we need to recursively call just the right part of the array as the complete array. It obviously won't always start with the base address (a), hence Options B, C and D are directly eliminated.

2 2 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.
1 1 vote

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.

​​​

Answer:
Position:
Show:

Related questions

50 50 votes
3 answers 3 answers
18.8k
18.8k views
Misbah Ghaya asked Feb 11, 2015
18,809 views
Which one of the following is the recurrence equation for the worst case time complexity of the quick sort algorithm for sorting $n\;( \geq 2)$ numbers? In the recurrenc...
84 84 votes
12 answers 12 answers
32.9k
32.9k views
go_editor asked Feb 12, 2015
32,862 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)$...
42 42 votes
5 answers 5 answers
17.4k
17.4k views
Kathleen asked Oct 9, 2014
17,441 views
Quick-sort is run on two inputs shown below to sort in ascending order taking first element as pivot$1, 2, 3, \dots n$$n, n-1, n-2, \dots, 2, 1$Let $C_1$ and $C_2$ be the...
74 74 votes
8 answers 8 answers
31.8k
31.8k views
Kathleen asked Sep 22, 2014
31,797 views
In quick-sort, for sorting $n$ elements, the $\left(n/4\right)^{th}$ smallest element is selected as pivot using an $O(n)$ time algorithm. What is the worst case time com...