Given an array arr[] and a number K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.
Expected Time Complexity: O(n)
//Here we have a function that gives the kth smallest element.
1: int kthSmallest(int arr[], int n, int k)
2: {
3:
4: sort(arr,arr+n);
5: return(arr[k-1]);
6: }
1: int kthSmallest(int arr[], int n, int k)
2: {
3:
4: sort(arr,arr+n,greater());
5: return(arr[k-1]);
6: }
0 Comments
If you have any doubt then let me know.