2
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 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

  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)

I found here a nice explanation about "How to find the kth largest element in an unsorted array of length n in O(n)?"

I've read partition , used in quick sort .Answer is given option (1).I agree with answer . But I need formal explanation .

Can you explain little bit please ?


Edit : AFAIK , Partition algorithm puts the chosen pivot in its correct position . We need recursively partition algorithm to find kth smallest element in an array .Partition algorithm run on a single side of array , either left or right of it's sorted pivot. I got stuck here . I'm thinking , it depends on kth index number ?

1
How can you "agree with answer" if you don't know why? Please add your current explanation and people will help you to complete it.hexasoft
Your question is about to prove that it is O(n)?hexasoft
No , I don't need complexity . I want to understand in English for my given algorithm .user4791206
the answer below from @vish4071 seems quite complete no?hexasoft
Nice explanation sir , but I need for my given code .user4791206

1 Answers

2
votes

Its simple. Say, you pick a q th largest element of the array. In that case, partition has q-1 elements in left half and n-q elements in the right half, while, q th element is the pivot. Now, 3 possibilities:

  • If q is k, you get the answer, which is your return statement.
  • If q > k, then k th element is in the left half of the array, and, in the left half, it is, still, the k th largest element. So, in the partition, we pass left half of the array, and k, that we have to find k th largest element there.
  • If q < k, then, k th largest element in in the right half of the array. Also, since there are q elements smaller than smallest element of this right part, k th largest element in the original array is k - q th largest in the right array. So, we pass the right array, and k-q, to find k-q th largest element of the partition.

EDIT:

Adding comments to your code:

int partition(int a[], int n);    //breaks array into 2 parts, according to pivot (1st element of array), left is smaller and right is larger han pivot.

Now, your recursive algorithm:

int kth_smallest (int a[], int n, int k)
{
    int left_end = partition (a, n);    //get index of a[0] in sorted array a
    if (left_end+1==k) {           //kth largest element found
        return a[left_end];
    }
    if (left_end+1 > k) {     //k th largest element in left part of array, and is k th largest in the left part
        return kth_smallest (___________);
    } else {                  ////k th largest element in right part of array, and is (k - left_end) th largest in the right part
        return kth_smallest (___________);   
    }
}