To find the Kth smallest element in an array, use the Quickselect algorithm or sort the array and return the element at the Kth index.
- Select a pivot element from the array.
- Partition the array into two subarrays: one with elements smaller than the pivot, and one with elements larger.
- If the Kth element is in the smaller subarray, repeat the process with that subarray.
- If the Kth element is in the larger subarray, repeat the process with that subarray.
- Return the Kth smallest element.
function kthSmallest(arr, k) {
arr.sort((a, b) => a - b);
return arr[k - 1];
}
// Example usage
console.log(kthSmallest([7, 10, 4, 3, 20, 15], 4)); // Output: 10
console.log(kthSmallest([12, 3, 5, 7, 19], 2)); // Output: 5
This method has a time complexity of O(n log n) for sorting, where n is the length of the array.
Tags: intermediate, JavaScript, Arrays, Algorithm