Quick Select (k-th Smallest)
Find k-th smallest element in average O(n) using partitioning.
Visualization
Interactive visualization for Quick Select (k-th Smallest)
Quick Select (Kth Smallest Element)
Array:
• Time: O(n) average, O(n²) worst
• Selection algorithm - finds kth smallest
• Used in median finding, top-k problems
Interactive visualization with step-by-step execution
Implementation
1function quickSelect(arr: number[], k: number): number {
2 let left = 0, right = arr.length - 1;
3 while (true) {
4 const pivot = arr[right];
5 let i = left;
6 for (let j = left; j < right; j++) if (arr[j] <= pivot) { [arr[i], arr[j]] = [arr[j], arr[i]]; i++; }
7 [arr[i], arr[right]] = [arr[right], arr[i]];
8 if (i === k) return arr[i];
9 if (i < k) left = i + 1; else right = i - 1;
10 }
11}Deep Dive
Theoretical Foundation
Quickselect selects the k-th smallest element by partitioning the array around a pivot, recursing into the side that contains k. Average linear time follows from random pivots and linear partitioning.
Complexity
Time
O(n)
O(n)
O(n²)
Space
O(1)
Applications
Industry Use
Median/percentile estimation
Streaming analytics and dashboards
Top-k retrieval
Use Cases
Related Algorithms
Binary Search
Binary Search is one of the most efficient searching algorithms with O(log n) time complexity. It works on sorted arrays by repeatedly dividing the search space in half, eliminating half of the remaining elements with each comparison. This divide-and-conquer approach makes it exponentially faster than linear search for large datasets.
Linear Search
Linear Search, also known as Sequential Search, is the simplest searching algorithm that checks each element in a list sequentially until the target element is found or the end is reached. Despite its O(n) time complexity, it's the only option for unsorted data and remains practical for small datasets or when simplicity is crucial.
Jump Search
Jump Search is an efficient algorithm for sorted arrays that combines the benefits of linear and binary search. Instead of checking every element (linear) or dividing the array (binary), it jumps ahead by fixed steps of √n and then performs linear search within the identified block. This approach achieves O(√n) time complexity, making it faster than linear search while being simpler than binary search for certain applications.
Interpolation Search
Interpolation Search is an improved variant of binary search specifically optimized for uniformly distributed sorted arrays. Instead of always checking the middle element, it estimates the target's position based on the target value relative to the range of values, similar to how humans search a phone book. Achieves O(log log n) average time for uniformly distributed data, significantly faster than binary search's O(log n).