给定一个数组arr[]和一个数字K ,其中K小于数组的大小,我们需要找到给定数组中第 K 个最小的元素。给出数组元素可以重复(不限于distinct)。
例子:
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
我们也用其他方法讨论了这篇文章:
- 未排序数组中的第 K 个最小/最大元素 |设置 1
- 未排序数组中的第 K 个最小/最大元素 |组 2(预期线性时间)
- 未排序数组中的第 K 个最小/最大元素 |设置 3(最坏情况线性时间)
方法:这个想法是使用计数排序的概念。以下是步骤:
- 找到数组中的最大元素(比如maxE )并创建一个长度为maxE + 1的数组(比如freq[] )并将其初始化为零。
- 循环遍历给定数组中的所有元素并将元素的频率存储在freq[] 中。
- 遍历数组freq[]直到我们到达第K 个元素。
- 打印在上述步骤中达到的第 K 个元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Kth smallest
// element in Unsorted Array
int findKthSmallest(int arr[], int n, int k)
{
// Initialize the max Element as 0
int max = 0;
// Iterate arr[] and find the maximum
// element in it
for (int i = 0; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
// Frequenncy array to store
// the frequencies
int counter[max + 1] = { 0 };
// Counter variable
int smallest = 0;
// Counting the frequencies
for (int i = 0; i < n; i++)
{
counter[arr[i]]++;
}
// Iterate through the freq[]
for (int num = 1; num <= max; num++)
{
// Check if num is present
// in the array
if (counter[num] > 0) {
// Increment the counter
// with the frequency
// of num
smallest += counter[num];
}
// Checking if we have reached
// the Kth smallest element
if (smallest >= k)
{
// Return the Kth
// smallest element
return num;
}
}
}
// Driver Code
int main()
{
// Given array
int arr[] = { 7, 1, 4, 4, 20, 15, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 5;
// Function Call
cout << findKthSmallest(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the Kth smallest
// element in Unsorted Array
static int findKthSmallest(int[] arr, int n, int k)
{
// Initialize the max Element as 0
int max = 0;
// Iterate arr[] and find the maximum
// element in it
for (int i = 0; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
// Frequenncy array to store
// the frequencies
int[] counter = new int[max + 1];
// Counter variable
int smallest = 0;
// Counting the frequencies
for (int i = 0; i < n; i++)
{
counter[arr[i]]++;
}
// Iterate through the freq[]
for (int num = 1; num <= max; num++)
{
// Check if num is present
// in the array
if (counter[num] > 0)
{
// Increment the counter
// with the frequency
// of num
smallest += counter[num];
}
// Checking if we have reached
// the Kth smallest element
if (smallest >= k)
{
// Return the Kth
// smallest element
return num;
}
}
return -1;
}
// Driver code
public static void main(String[] args)
{
// Given array
int[] arr = { 7, 1, 4, 4, 20, 15, 8 };
int N = arr.length;
int K = 5;
// Function call
System.out.print(findKthSmallest(arr, N, K));
}
}
Python3
# Python3 program for the
# above approach
# Function to find the Kth
# smallest element in Unsorted
# Array
def findKthSmallest(arr, n, k):
# Initialize the max
# Element as 0
max = 0
# Iterate arr[] and find
# the maximum element in it
for i in range(n):
if (arr[i] > max):
max = arr[i]
# Frequenncy array to
# store the frequencies
counter = [0] * (max + 1)
# Counter variable
smallest = 0
# Counting the frequencies
for i in range(n):
counter[arr[i]] += 1
# Iterate through the freq[]
for num in range(1, max + 1):
# Check if num is present
# in the array
if (counter[num] > 0):
# Increment the counter
# with the frequency
# of num
smallest += counter[num]
# Checking if we have reached
# the Kth smallest element
if (smallest >= k):
# Return the Kth
# smallest element
return num
# Driver Code
if __name__ == "__main__":
# Given array
arr = [7, 1, 4, 4,
20, 15, 8]
N = len(arr)
K = 5
# Function Call
print(findKthSmallest(arr, N, K))
# This code is contributed by Chitranayal
C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the Kth smallest
// element in Unsorted Array
static int findKthSmallest(int[] arr,
int n, int k)
{
// Initialize the max
// Element as 0
int max = 0;
// Iterate []arr and find
// the maximum element in it
for (int i = 0; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
// Frequenncy array to store
// the frequencies
int[] counter = new int[max + 1];
// Counter variable
int smallest = 0;
// Counting the frequencies
for (int i = 0; i < n; i++)
{
counter[arr[i]]++;
}
// Iterate through the []freq
for (int num = 1;
num <= max; num++)
{
// Check if num is present
// in the array
if (counter[num] > 0)
{
// Increment the counter
// with the frequency
// of num
smallest += counter[num];
}
// Checking if we have reached
// the Kth smallest element
if (smallest >= k)
{
// Return the Kth
// smallest element
return num;
}
}
return -1;
}
// Driver code
public static void Main(String[] args)
{
// Given array
int[] arr = {7, 1, 4, 4,
20, 15, 8};
int N = arr.Length;
int K = 5;
// Function call
Console.Write(findKthSmallest(arr,
N, K));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
8
时间复杂度: O(N) ,其中 N 是给定数组中的元素数。
辅助空间: O(M)其中 M 是给定数组中的最大元素。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live