Javatpoint Logo
Javatpoint Logo

Write Python Program to find the Kth Smallest Element

The problem is a given an integer array, we need to find the kth smallest element in the array where k is a positive integer less than or equal to the length of the array. Let's see the following example.

Example -

Input:

Output:

k'th smallest array element is 4

Input:

Output:

k'th smallest array element is 10 

Let's solve this problem using the various methods.

Method - 1: Brute Force Approach

In this approach, first, we will sort the given array. Here we will use the bubble sort algorithm and gets the Kth smallest element using list slicing. Let's understand the following example.

Example -

Output:

kth smallest array element is: 7

Explanation -

In the above function, we define a function called find_k_smallest() that finds the k-th smallest element in an array using a simple sorting approach.

  1. The function find_k_smallest takes three parameters: arr (the input array), n (the length of the array), and k (the position of the desired k-th smallest element).
  2. The code uses nested loops to perform a simple sorting operation on the array. The outer loop iterates over each element in the array, starting from the first element (i = 0).
  3. The inner loop iterates over the remaining elements in the array, starting from i to the end of the array. It compares the element at index i with the subsequent elements (j) to find the smallest element.
  4. If the element at index i is greater than the element at index j, a swap is performed to bring the smaller element to the front. This way, after the loop completes, the smallest element will be at index i (the first element in the array).
  5. The outer loop continues until it reaches the end of the array, ensuring that the array is sorted in ascending order.
  6. Finally, the function returns the k-th smallest element from the sorted array. Since array indexing starts from 0, the element at index k-1 is returned (arr[k-1]).

In the given example, the input array is [7, 10, 4, 3, 20, 15], and the desired k-th smallest element is the 3rd smallest (k = 3). After running the find_k_smallest() function, the output will be 7, which is the 3rd smallest element in the sorted array.

Hence this implementation has a time complexity of O(n2) because of the nested loops used for sorting. However, we will optimize the above solution.

Method - 2 Using Sort Method

We will use the built-in sort() method. Let's understand the following example.

Example -

Output:

kth smallest array element is: 7

Explanation -

The function find_k_smallest() takes three parameters: arr (the input array), n (the length of the array), and k (the position of the desired k-th smallest element).

We use the sort() method to sort the array in ascending order. This rearranges the elements of the array such that the smallest element is at index 0 and the largest element is at the last index.

After sorting the array, the function returns the element at index k-1. Since array indexing starts from 0, the k-th smallest element will be at index k-1.

The overall time complexity of the above code is O(n log n), where n is the length of the array.

Method - 3: Using Max Heap

We will solve this problem using the min heap. Let's understand the following example.

Example -

Output:

kth smallest array element is:7

Explanation -

In the above code, we import the heapq module, to work with the heap. Then, we create the empty heap to store the elements of the array. After pushing all elements onto the heap, we enter a loop to extract the smallest element k times. We use heapq.heappop() to pop and retrieve the smallest element from the heap.

The kth_smallest variable keeps track of the k-th smallest element as we extract elements from the heap.

Finally, we return the value of kth_smallest, which will be the k-th smallest element in the array.

The time complexity of the above code is O(n) which is an optimized version of previous codes.

Method - 4: Using Priority Queue

To solve the problem of finding the k-th smallest element using a priority queue, we can utilize Python's queue module, specifically the PriorityQueue class. Here's an implementation using a priority queue. Let's understand the following example.

Example -

Output:

kth smallest array element is: 7

Explanation -

In the above code, first we import the PriorityQueue class from the queue module, which allows us to work with a priority queue.

The find_k_smallest function takes two parameters: arr (the input array) and k (the position of the desired k-th smallest element). We create a new pq instance of the PriorityQueue class. We iterate over each element in the array and use the put() method to insert the element into the priority queue.

After inserting an element, we check if the size of the priority queue (pq.qsize()) is greater than k. If it is, we remove the smallest element from the priority queue using the get() method. This ensures that the priority queue only contains the k smallest elements at any given time.

After processing all elements, we use the get() method to retrieve the k-th smallest element from the priority queue.

Finally, we return the retrieved k-th smallest element.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA