How to find the kth largest element in an unsorted array of length n in On?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding the k-th largest element in an unsorted array is a common problem in computer science. A direct approach might be to sort the array and then directly pick the k-th element, but this costs time complexity. However, by employing more advanced techniques, such as the Quickselect algorithm, we can solve this problem in average time.
Quickselect Algorithm
Overview
Quickselect is an efficient selection algorithm developed by Tony Hoare, who is also known for creating Quicksort. It is essentially a partial sorting algorithm that focuses on finding the k-th smallest or largest element without fully sorting the data.
The core idea of Quickselect is similar to that of Quicksort:
- Choose a pivot: A pivot is chosen from the array.
- Partitioning: Reorder the array so that all elements lesser than the pivot come before it, and all elements greater come after it.
- Recursive Selection: Based on the position of the pivot, decide whether to recursively apply the algorithm to the left or right partition.
How it Works
To find the k-th largest element in an unsorted array, convert the problem to finding the (n-k)-th smallest element since it's more intuitive to reason about finding the smallest elements.
Consider an unsorted array arr of length n and an integer k:
Example
Consider the array arr = [3, 2, 1, 5, 6, 4] and we want to find the 2nd largest element.
- Initial Call:
quickselect(arr, 0, 5, 4)(as 6 - 2 = 4) - Partition Step: Assuming pivot selected is 4, reorder to
[3, 2, 1, 4, 6, 5]. The pivot index is 3. - Recursive Selection:
- Because
4(pivot index) <5(k), recurse into right subarray[6, 5].
- Partition Step: Select
5as a pivot, reordering to[5, 6]. - Recursive Selection:
5is now at the 4-th index. Return5.
Hence, the 2nd largest number is 5.
Time Complexity
Quickselect, similar to Quicksort, has different time complexities based on the selection of pivot:
- Best/Average Case: . The algorithm works by partitioning the array, where ideally the pivot divides the array into roughly equal parts, leading to a logarithmic number of recursive calls.
- Worst Case: . This occurs when the smallest or largest element is always chosen as the pivot, leading to skewed partitioning. Randomized pivot selection or using the median of three can help alleviate this issue.
Key Comparisons
Comparing Quickselect to other strategies such as sorting the entire array:
| Strategy | Time Complexity | Space Complexity | Notes |
| Full Sort | in-place / external | Good for multiple selections | |
| Quickselect | Average: / Worst: | if in-place | Optimal for single selection in average case |
Additional Techniques
Randomized Quickselect
Randomly selecting a pivot helps avoid the worst case in Quickselect. Randomization ensures the algorithm is effective statistically.
Heap Implementation
A min-heap or max-heap can be utilized to find the k-th largest element. Build a min-heap of size k, and iterate over the array, keeping the largest k elements in the heap. This achieves time complexity and is useful when k is small relative to n.
Conclusion
Finding the k-th largest element in an unsorted array efficiently is a solved problem in computer science. By leveraging Quickselect, you can achieve average linear time complexity. Understanding and implementing Quickselect correctly is crucial for optimizing search operations in large datasets.
Related reading
- How to find the kth smallest element in the union of two sorted arrays?
- How to find the Largest Difference in an Array
- how to find the least number of operations to compute xn
- How to find the length of a linked list that is having cycles in it?
- How to find the lowest common ancestor of two nodes in any binary tree?
- How to find the max distance between a set of nodes on a tree?
- How to find the lexicographically smallest string by reversing a substring?
- How to find the minimum number of moves to move an item into a position in a stack?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.