mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Add KthElementFinder algorithm (#5836)
This commit is contained in:
@ -174,6 +174,7 @@
|
||||
* [GenericHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java)
|
||||
* [Heap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/Heap.java)
|
||||
* [HeapElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/HeapElement.java)
|
||||
* [KthElementFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/KthElementFinder.java)
|
||||
* [LeftistHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java)
|
||||
* [MaxHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java)
|
||||
* [MinHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java)
|
||||
@ -848,6 +849,7 @@
|
||||
* heaps
|
||||
* [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
|
||||
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
|
||||
* [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
|
||||
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
|
||||
* [MinPriorityQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java)
|
||||
* lists
|
||||
|
@ -0,0 +1,60 @@
|
||||
|
||||
package com.thealgorithms.datastructures.heaps;
|
||||
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
/**
|
||||
* This class provides methods to find the Kth largest or Kth smallest element
|
||||
* in an array using heaps. It leverages a min-heap to find the Kth largest element
|
||||
* and a max-heap to find the Kth smallest element efficiently.
|
||||
*
|
||||
* @author Hardvan
|
||||
*/
|
||||
public final class KthElementFinder {
|
||||
private KthElementFinder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the Kth largest element in the given array.
|
||||
* Uses a min-heap of size K to track the largest K elements.
|
||||
*
|
||||
* Time Complexity: O(n * log(k)), where n is the size of the input array.
|
||||
* Space Complexity: O(k), as we maintain a heap of size K.
|
||||
*
|
||||
* @param nums the input array of integers
|
||||
* @param k the desired Kth position (1-indexed, i.e., 1 means the largest element)
|
||||
* @return the Kth largest element in the array
|
||||
*/
|
||||
public static int findKthLargest(int[] nums, int k) {
|
||||
PriorityQueue<Integer> minHeap = new PriorityQueue<>(k);
|
||||
for (int num : nums) {
|
||||
minHeap.offer(num);
|
||||
if (minHeap.size() > k) {
|
||||
minHeap.poll();
|
||||
}
|
||||
}
|
||||
return minHeap.peek();
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the Kth smallest element in the given array.
|
||||
* Uses a max-heap of size K to track the smallest K elements.
|
||||
*
|
||||
* Time Complexity: O(n * log(k)), where n is the size of the input array.
|
||||
* Space Complexity: O(k), as we maintain a heap of size K.
|
||||
*
|
||||
* @param nums the input array of integers
|
||||
* @param k the desired Kth position (1-indexed, i.e., 1 means the smallest element)
|
||||
* @return the Kth smallest element in the array
|
||||
*/
|
||||
public static int findKthSmallest(int[] nums, int k) {
|
||||
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
|
||||
for (int num : nums) {
|
||||
maxHeap.offer(num);
|
||||
if (maxHeap.size() > k) {
|
||||
maxHeap.poll();
|
||||
}
|
||||
}
|
||||
return maxHeap.peek();
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.thealgorithms.datastructures.heaps;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class KthElementFinderTest {
|
||||
@Test
|
||||
public void testFindKthLargest() {
|
||||
int[] nums = {3, 2, 1, 5, 6, 4};
|
||||
assertEquals(5, KthElementFinder.findKthLargest(nums, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindKthSmallest() {
|
||||
int[] nums = {7, 10, 4, 3, 20, 15};
|
||||
assertEquals(7, KthElementFinder.findKthSmallest(nums, 3));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user