This method is based on the idea of partially sorting the array using bubble sort, but only for the first k passes, so that the k smallest elements bubble to the front.
classSolution {
public:int findKthSmallest(vector<int>& nums, int k) {
int n = nums.size();
for (int i =0; i < k; ++i) {
for (int j =0; j < n - i -1; ++j) {
if (nums[j] > nums[j +1]) {
swap(nums[j], nums[j +1]);
}
}
}
return nums[k -1];
}
};
classSolution {
publicintfindKthSmallest(int[] nums, int k) {
int n = nums.length;
for (int i = 0; i < k; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (nums[j]> nums[j + 1]) {
int temp = nums[j];
nums[j]= nums[j + 1];
nums[j + 1]= temp;
}
}
}
return nums[k - 1];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funfindKthSmallest(nums: IntArray, k: Int): Int {
val n = nums.size
for (i in0 until k) {
for (j in0 until n - i - 1) {
if (nums[j] > nums[j + 1]) {
val temp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = temp
}
}
}
return nums[k - 1]
}
}
1
2
3
4
5
6
7
deffind_kth_smallest(nums: list[int], k: int) -> int:
n = len(nums)
for i in range(k):
for j in range(n - i -1):
if nums[j] > nums[j +1]:
nums[j], nums[j +1] = nums[j +1], nums[j]
return nums[k -1]
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnfind_kth_smallest(nums: &mut Vec<i32>, k: i32) -> i32 {
let n = nums.len();
for i in0..k asusize {
for j in0..n - i -1 {
if nums[j] > nums[j +1] {
nums.swap(j, j +1);
}
}
}
nums[(k -1) asusize]
}
}
funcfindKthSmallest(nums []int, kint) int {
ifk<=0||k > len(nums) {
return0 }
maxHeap:=&IntHeap{}
heap.Init(maxHeap)
fori:=0; i < k; i++ {
heap.Push(maxHeap, nums[i])
}
fori:=k; i < len(nums); i++ {
ifnums[i] < (*maxHeap)[0] {
heap.Pop(maxHeap)
heap.Push(maxHeap, nums[i])
}
}
return (*maxHeap)[0]
}
// IntHeap is a type that implements heap.Interface for a slice of integers.typeIntHeap []intfunc (hIntHeap) Len() int { return len(h) }
func (hIntHeap) Less(i, jint) bool { returnh[i] > h[j] } // max-heapfunc (hIntHeap) Swap(i, jint) { h[i], h[j] = h[j], h[i] }
func (h*IntHeap) Push(xinterface{}) {
*h = append(*h, x.(int))
}
func (h*IntHeap) Pop() interface{} {
old:=*hn:= len(old)
x:=old[n-1]
*h = old[0 : n-1]
returnx}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
publicintfindKthSmallest(int[] nums, int k) {
PriorityQueue<Integer> maxHeap =new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < k; i++) {
maxHeap.offer(nums[i]);
}
for (int i = k; i < nums.length; i++) {
if (nums[i]< maxHeap.peek()) {
maxHeap.poll();
maxHeap.offer(nums[i]);
}
}
return maxHeap.peek();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
funfindKthSmallest(nums: IntArray, k: Int): Int {
val maxHeap = PriorityQueue<Int>(Collections.reverseOrder())
for (i in nums.indices) {
maxHeap.add(nums[i])
if (maxHeap.size > k) {
maxHeap.poll()
}
}
return maxHeap.peek() ?: -1 }
}
1
2
3
4
5
6
7
8
9
import heapq
deffind_kth_smallest(nums: list[int], k: int) -> int:
max_heap = nums[:k]
heapq._heapify_max(max_heap)
for num in nums[k:]:
if num < max_heap[0]:
heapq._heapreplace_max(max_heap, num)
return max_heap[0]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::collections::BinaryHeap;
impl Solution {
pubfnfind_kth_smallest(nums: &mut Vec<i32>, k: i32) -> i32 {
letmut max_heap = BinaryHeap::new();
for&num in nums.iter().take(k asusize) {
max_heap.push(num);
}
for&num in nums.iter().skip(k asusize) {
if num <*max_heap.peek().unwrap() {
max_heap.pop();
max_heap.push(num);
}
}
*max_heap.peek().unwrap()
}
}
import"container/heap"funcfindKthSmallest(nums []int, kint) int {
maxHeap:=&IntHeap{}
heap.Init(maxHeap)
fori, num:=rangenums {
ifi < k {
heap.Push(maxHeap, num)
} elseifnum < (*maxHeap)[0] {
heap.Pop(maxHeap)
heap.Push(maxHeap, num)
}
}
return (*maxHeap)[0]
}
// IntHeap is a type that implements heap.Interface for a slice of integers.typeIntHeap []intfunc (hIntHeap) Len() int { return len(h) }
func (hIntHeap) Less(i, jint) bool { returnh[i] > h[j] } // max-heapfunc (hIntHeap) Swap(i, jint) { h[i], h[j] = h[j], h[i] }
func (h*IntHeap) Push(xinterface{}) {
*h = append(*h, x.(int))
}
func (h*IntHeap) Pop() interface{} {
old:=*hn:= len(old)
x:=old[n-1]
*h = old[0 : n-1]
returnx}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.PriorityQueue;
classSolution {
publicintfindKthSmallest(int[] nums, int k) {
PriorityQueue<Integer> maxHeap =new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < k; i++) {
maxHeap.offer(nums[i]);
}
for (int i = k; i < nums.length; i++) {
if (nums[i]< maxHeap.peek()) {
maxHeap.poll();
maxHeap.offer(nums[i]);
}
}
return maxHeap.peek();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funfindKthSmallest(nums: IntArray, k: Int): Int {
val maxHeap = PriorityQueue<Int>(k, Collections.reverseOrder())
for (i in nums.indices) {
if (i < k) {
maxHeap.add(nums[i])
} elseif (nums[i] < maxHeap.peek()!!) {
maxHeap.poll()
maxHeap.add(nums[i])
}
}
return maxHeap.peek()!! }
}
1
2
3
4
5
6
7
8
9
import heapq
deffind_kth_smallest(nums: list[int], k: int) -> int:
max_heap = [-x for x in nums[:k]]
heapq.heapify(max_heap)
for num in nums[k:]:
if-num > max_heap[0]:
heapq.heapreplace(max_heap, -num)
return-max_heap[0]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::collections::BinaryHeap;
impl Solution {
pubfnfind_kth_smallest(nums: &mut Vec<i32>, k: i32) -> i32 {
letmut max_heap = BinaryHeap::new();
for&num in nums.iter().take(k asusize) {
max_heap.push(num);
}
for&num in nums.iter().skip(k asusize) {
if num <*max_heap.peek().unwrap() {
max_heap.pop();
max_heap.push(num);
}
}
*max_heap.peek().unwrap()
}
}