K-th Smallest Element in an Unsorted Array using Priority Queue


A priority queue is a heap-based data structure, stores elements in such a way that the greatest or smallest element always be present on the top. We are given an array that is unsorted and we have to find the Kth smallest element from it using the Priority Queue. Here, the element k will be given and it must be in the range from 1 to the size of the given array.

Example

Let's understand the problem with the help of an input-output example −

Input

int arr[] = {1, 5, 6, 2, 3, 6, 7, 9, 12, 15, 0, 8}
int k = 4

Output

3

Explanation

In the given array, after sorting the array we will get the 3 as the 4th smallest element of the array.

Approach 1

In this approach, we are going to create a function that will take a given array and number as the parameter and will return the kth smallest element as the return value.

We will traverse over the array and fill all the elements of the array to the priority queue in N * log(N) time.

Then, we will remove all the elements of the priority queue until its size becomes k and we will use the while loop and pop() method of the priority queue to do this.

In the end, we will return the top element of the priority queue of size K, as it will be the required number.

Example

#include <bits/stdc++.h>
using namespace std;

// function to get the kth smallest element 
int findNumber(vector<int>& arr, int k){
   // defining priority queue 
   priority_queue<int>pq;    
   // adding all the elements of the given array to it
   int len = arr.size();
   for(int i=0; i<len; i++){
      pq.push(arr[i]);
   }    
   // now size of the priority queue is N
   // remove all the elements until size is K
   while(pq.size() > k){
      pq.pop();
   }
   // return the final answer 
   return pq.top();
}
int main(){
   vector<int> arr = {1, 5, 6, 2, 3, 6, 7, 9, 12, 15, 0, 8};
   int k = 4; // given k 
   // calling the function 
   cout<< " The kth smallest number in the given array is: " << findNumber(arr,k) << endl;
   return 0;
}

Output

The kth smallest number in the given array is: 3

Time and Space Complexity

The time complexity of the above code is O(N*log(N)), where N is the size of the given array.

The space complexity of the above code is O(N), as we are using the priority queue of max size N.

Approach 2

This approach is similar to the previous one but the difference is it only takes less time complexity with a logarithmic factor of K and size of priority queue will also be K, not N.

We will traverse over the array and add all the elements of the array to the priority queue until the size of the priority queue is less than K.

After that, we will check if the current array element is greater as compared to the top element of the priority queue then we can ignore the current element as already queue have the smaller elements.

If the current element is less than the top element then the top element is greatest among all K elements present in the priority queue, so we will remove the top element and add the current element to the priority queue.

In the end, we will return the top element of the priority queue as it is the greatest among all.

Example

#include <bits/stdc++.h>
using namespace std;

// function to get the k-th smallest element 
int findNumber(vector<int>& arr, int k){
   // defining priority queue 
   priority_queue<int>pq;
    
   // adding all the elements of the given array to the priority queue 
   int len = arr.size();
   for(int i=0; i<len; i++){
      if(pq.size() < k){
         // if total number of elements in the priority queue is less than k
         // then add this element to the queue
         pq.push(arr[i]);
      } else if(pq.top() < arr[i]){
         // if the top element of the priority queue is smaller than this current element. then just move to the next element
         continue;
      } else {
         // else add this element to the queue 
         pq.pop();
         pq.push(arr[i]);
      }
   }
   return pq.top();
}
int main(){
   // given array
   vector<int> arr = {1, 5, 6, 2, 3, 6, 7, 9, 12, 15, 0, 8};
   int k = 4; // given k 
   // calling the function 
   cout<<"The kth smallest number in the given array is: "<<findNumber(arr,k)<<endl;
   return 0;
}

Output

The kth smallest number in the given array is: 3

Time and Space Complexity

The time complexity of the above code is O(N*log(K)), where N is the size of the given array and K is the given number, this log factor is due to the priority queue and its size could max go up to k here.

The space complexity of the above code is O(K), as we are just storing the maximum k element in the priority queue.

Conclusion

In this tutorial, we have implemented a program to find the Kth smallest element from the given array by using a priority queue. A priority queue is a data structure based on the heap and stores the smallest or greatest element on the top. We have implemented two programs with the first program of O(N*log(N)) time and O(N) space complexity, while the second program has the factor of K in both time and space complexity.

Updated on: 24-Aug-2023

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements