Kth smallest or largest element in unsorted Array using Counting Sort
Given an array arr[] and a number K, where K is smaller than the size of the array, we need to find the Kth smallest element in the given array. It is given that array elements can be repeated (not limited to distinct).
Examples:
Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3
Output: 7
Input: arr[] = {7, 1, 5, 4, 20, 15, 8}, K = 5
Output: 8
We have discussed this article with other approaches as well:
Approach: The idea is to use the concept of Counting Sort. Below are the steps:
- Find the maximum element(say maxE) in the array and create an array(say freq[]) of length maxE + 1 and initialize it to zero.
- Loop through all the elements in the given array and store the frequency of the element in freq[].
- Iterate over the array freq[] until we reach the Kth element.
- Print the Kth element reached in the above step.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int findKthSmallest( int arr[], int n, int k)
{
int max = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
int counter[max + 1] = { 0 };
int smallest = 0;
for ( int i = 0; i < n; i++)
{
counter[arr[i]]++;
}
for ( int num = 1; num <= max; num++)
{
if (counter[num] > 0) {
smallest += counter[num];
}
if (smallest >= k)
{
return num;
}
}
}
int main()
{
int arr[] = { 7, 1, 4, 4, 20, 15, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 5;
cout << findKthSmallest(arr, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findKthSmallest( int [] arr, int n, int k)
{
int max = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
int [] counter = new int [max + 1 ];
int smallest = 0 ;
for ( int i = 0 ; i < n; i++)
{
counter[arr[i]]++;
}
for ( int num = 1 ; num <= max; num++)
{
if (counter[num] > 0 )
{
smallest += counter[num];
}
if (smallest >= k)
{
return num;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int [] arr = { 7 , 1 , 4 , 4 , 20 , 15 , 8 };
int N = arr.length;
int K = 5 ;
System.out.print(findKthSmallest(arr, N, K));
}
}
|
Python3
def findKthSmallest(arr, n, k):
max = 0
for i in range (n):
if (arr[i] > max ):
max = arr[i]
counter = [ 0 ] * ( max + 1 )
smallest = 0
for i in range (n):
counter[arr[i]] + = 1
for num in range ( 1 , max + 1 ):
if (counter[num] > 0 ):
smallest + = counter[num]
if (smallest > = k):
return num
if __name__ = = "__main__" :
arr = [ 7 , 1 , 4 , 4 ,
20 , 15 , 8 ]
N = len (arr)
K = 5
print (findKthSmallest(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findKthSmallest( int [] arr,
int n, int k)
{
int max = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
int [] counter = new int [max + 1];
int smallest = 0;
for ( int i = 0; i < n; i++)
{
counter[arr[i]]++;
}
for ( int num = 1;
num <= max; num++)
{
if (counter[num] > 0)
{
smallest += counter[num];
}
if (smallest >= k)
{
return num;
}
}
return -1;
}
public static void Main(String[] args)
{
int [] arr = {7, 1, 4, 4,
20, 15, 8};
int N = arr.Length;
int K = 5;
Console.Write(findKthSmallest(arr,
N, K));
}
}
|
Javascript
<script>
function findKthSmallest(arr, n, k)
{
let max = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
let counter = Array.from({length: max + 1}, (_, i) => 0);
let smallest = 0;
for (let i = 0; i < n; i++)
{
counter[arr[i]]++;
}
for (let num = 1; num <= max; num++)
{
if (counter[num] > 0)
{
smallest += counter[num];
}
if (smallest >= k)
{
return num;
}
}
return -1;
}
let arr = [ 7, 1, 4, 4, 20, 15, 8 ];
let N = arr.length;
let K = 5;
document.write(findKthSmallest(arr, N, K));
</script>
|
Time complexity: O(N+MaxElement) where N is the number of elements in the given array, and MaxElement is the maximum number in the array. Pseudo Linear-Time complexity.
Auxiliary Space: O(M) where M is the maximum element in the given array.
Last Updated :
02 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...