K’th Smallest/Largest Element in Unsorted Array | Worst case Linear Time
Last Updated :
23 Jul, 2025
Given an array of distinct integers arr[] and an integer k. The task is to find the k-th smallest element in the array. For better understanding, k refers to the element that would appear in the k-th position if the array were sorted in ascending order.
Note: k will always be less than the size of the array.
Examples:
Input: arr[] = [7, 10, 4, 3, 20, 15], k = 3
Output: 7
Explanation: The sorted array is [3, 4, 7, 10, 15, 20]. The 3rd smallest element is 7.
Input: arr[] = [12, 3, 5, 7, 19], k = 2
Output: 5
Explanation: The sorted array is [3, 5, 7, 12, 19]. The 2nd smallest element is 5.
Input: arr[] = [1, 5, 2, 8, 3], k = 4
Output: 5
In the previous post, we explored an algorithm with expected linear time complexity. In this post, a worst-case linear time we method is discussed.
Approach:
The intuition of this code starts with the same base idea as QuickSelect(), to find the k-th smallest element by partitioning the array around a pivot. But unlike QuickSelect, which may choose a bad pivot and degrade to O(n²) in the worst case, this algorithm ensures worst-case linear time by carefully choosing a pivot using the Median of Medians technique.
We want a pivot that guarantees a reasonably balanced partition, not perfectly balanced, but not extremely skewed either. That means the pivot should ensure that a significant portion of the array lies on both sides of it. This is where the Median of Medians strategy comes in
Steps to implement the above idea:
- To find a good pivot, we divide the array into groups of 5 elements. This size (5) is a key observation, it is small enough to allow fast sorting and large enough to ensure a mathematically provable balance during partitioning.
- Each group is sorted independently and its median is collected into a new list called medians.
- Once we gather all medians, we recursively find the median of this median list. This value becomes our pivot. The purpose is to avoid bad pivots by using a value that is likely close to the true median of the entire array.
- After determining the pivot, we partition the array using the standard logic (elements <= pivot on the left, > pivot on the right). The function partitionAroundPivot() moves the pivot to its correct position and returns that position.
- Now, we compare the position of this pivot with the desired k-th index. If it matches, we return that value directly as our answer.
- If not, we decide whether to recurse on the left or right side of the pivot:
- If the pivot lies after the k-th position, the answer lies in the left subarray.
- If the pivot lies before the k-th position, we adjust k accordingly and recurse on the right subarray.
C++
// C++ implementation of the Worst Case Linear Time algorithm
// to find the k-th smallest element using Median of Medians
#include <bits/stdc++.h>
using namespace std;
// Returns median of a small group (size <= 5)
int getMedian(vector<int> &group) {
sort(group.begin(), group.end());
return group[group.size() / 2];
}
// Function to Partition array from index
// l to r around the pivot value x
int partitionAroundPivot(vector<int> &arr,
int l, int r, int x) {
// Move pivot x to end
int i;
for (i = l; i < r; i++) {
if (arr[i] == x) break;
}
swap(arr[i], arr[r]);
// Standard partition logic
i = l;
for (int j = l; j < r; j++) {
if (arr[j] <= x) {
swap(arr[i], arr[j]);
i++;
}
}
swap(arr[i], arr[r]);
// Final position of pivot
return i;
}
// Recursively finds the k-th smallest element in arr[l..r]
int selectKthSmallest(vector<int> &arr, int l, int r, int k) {
if (k > 0 && k <= r - l + 1) {
int n = r - l + 1;
vector<int> medians;
int i;
// Divide array into groups of 5 and store their medians
for (i = 0; i < n / 5; i++) {
vector<int> group(arr.begin() + l + i * 5,
arr.begin() + l + i * 5 + 5);
medians.push_back(getMedian(group));
}
// Handle the last group with less than 5 elements
if (i * 5 < n) {
vector<int> lastGroup(arr.begin() + l + i * 5,
arr.begin() + l + i * 5 + (n % 5));
medians.push_back(getMedian(lastGroup));
}
// Find median of medians
int pivot;
if (medians.size() == 1) {
pivot = medians[0];
} else {
pivot = selectKthSmallest(medians, 0, medians.size() - 1,
medians.size() / 2);
}
// Partition array and get position of pivot
int pos = partitionAroundPivot(arr, l, r, pivot);
// If position matches k, return result
if (pos - l == k - 1) return arr[pos];
// Recur on left or right part accordingly
if (pos - l > k - 1)
return selectKthSmallest(arr, l, pos - 1, k);
return selectKthSmallest(arr, pos + 1, r, k - pos + l - 1);
}
return INT_MAX;
}
// Function to find kth Smallest in Array
int kthSmallest(vector<int> &arr, int k) {
return selectKthSmallest(arr, 0, arr.size() - 1, k);
}
// Driver code
int main() {
vector<int> arr = {7, 10, 4, 3, 20, 15};
int k = 3;
cout << kthSmallest(arr, k);
return 0;
}
Java
// Java implementation of the Worst Case Linear Time algorithm
// to find the k-th smallest element using Median of Medians
import java.util.*;
class GfG {
// Returns median of a small group (size <= 5)
static int getMedian(int[] group) {
Arrays.sort(group);
return group[group.length / 2];
}
// Function to Partition array from index
// l to r around the pivot value x
static int partitionAroundPivot(int[] arr, int l, int r, int x) {
// Move pivot x to end
int i;
for (i = l; i < r; i++) {
if (arr[i] == x) break;
}
int temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
// Standard partition logic
i = l;
for (int j = l; j < r; j++) {
if (arr[j] <= x) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
i++;
}
}
int t = arr[i];
arr[i] = arr[r];
arr[r] = t;
// Final position of pivot
return i;
}
// Recursively finds the k-th smallest element in arr[l..r]
static int selectKthSmallest(int[] arr, int l, int r, int k) {
if (k > 0 && k <= r - l + 1) {
int n = r - l + 1;
ArrayList<Integer> medians = new ArrayList<>();
int i;
// Divide array into groups of 5 and store their medians
for (i = 0; i < n / 5; i++) {
int[] group = Arrays.copyOfRange(arr, l + i * 5, l + i * 5 + 5);
medians.add(getMedian(group));
}
// Handle the last group with less than 5 elements
if (i * 5 < n) {
int[] lastGroup = Arrays.copyOfRange(arr, l + i * 5, l + i * 5 + (n % 5));
medians.add(getMedian(lastGroup));
}
// Find median of medians
int pivot;
if (medians.size() == 1) {
pivot = medians.get(0);
} else {
int[] medArr = new int[medians.size()];
for (int j = 0; j < medians.size(); j++) medArr[j] = medians.get(j);
pivot = selectKthSmallest(medArr, 0, medArr.length - 1, medArr.length / 2);
}
// Partition array and get position of pivot
int pos = partitionAroundPivot(arr, l, r, pivot);
// If position matches k, return result
if (pos - l == k - 1) return arr[pos];
// Recur on left or right part accordingly
if (pos - l > k - 1)
return selectKthSmallest(arr, l, pos - 1, k);
return selectKthSmallest(arr, pos + 1, r, k - pos + l - 1);
}
return Integer.MAX_VALUE;
}
// Function to find kth Smallest in Array
static int kthSmallest(int[] arr, int k) {
return selectKthSmallest(arr, 0, arr.length - 1, k);
}
public static void main(String[] args) {
int[] arr = {7, 10, 4, 3, 20, 15};
int k = 3;
System.out.println(kthSmallest(arr, k));
}
}
Python
# Python implementation of the Worst Case Linear Time algorithm
# to find the k-th smallest element using Median of Medians
# Returns median of a small group (size <= 5)
def getMedian(group):
group.sort()
return group[len(group) // 2]
# Function to Partition array from index
# l to r around the pivot value x
def partitionAroundPivot(arr, l, r, x):
# Move pivot x to end
for i in range(l, r):
if arr[i] == x:
break
arr[i], arr[r] = arr[r], arr[i]
# Standard partition logic
i = l
for j in range(l, r):
if arr[j] <= x:
arr[i], arr[j] = arr[j], arr[i]
i += 1
arr[i], arr[r] = arr[r], arr[i]
# Final position of pivot
return i
# Recursively finds the k-th smallest element in arr[l..r]
def selectKthSmallest(arr, l, r, k):
if k > 0 and k <= r - l + 1:
n = r - l + 1
medians = []
# Divide array into groups of 5 and store their medians
i = 0
while i < n // 5:
group = arr[l + i * 5: l + i * 5 + 5]
medians.append(getMedian(group))
i += 1
# Handle the last group with less than 5 elements
if i * 5 < n:
lastGroup = arr[l + i * 5: l + i * 5 + (n % 5)]
medians.append(getMedian(lastGroup))
# Find median of medians
if len(medians) == 1:
pivot = medians[0]
else:
pivot = selectKthSmallest(medians, 0, len(medians) - 1,
len(medians) // 2 + 1)
# Partition array and get position of pivot
pos = partitionAroundPivot(arr, l, r, pivot)
# If position matches k, return result
if pos - l == k - 1:
return arr[pos]
# Recur on left or right part accordingly
if pos - l > k - 1:
return selectKthSmallest(arr, l, pos - 1, k)
return selectKthSmallest(arr, pos + 1, r,
k - pos + l - 1)
return float('inf')
# Function to find kth Smallest in Array
def kthSmallest(arr, k):
return selectKthSmallest(arr, 0, len(arr) - 1, k)
# Driver code
if __name__ == "__main__":
arr = [7, 10, 4, 3, 20, 15]
k = 3
print(kthSmallest(arr, k))
C#
// C# implementation of the Worst Case Linear Time algorithm
// to find the k-th smallest element using Median of Medians
using System;
using System.Collections.Generic;
class GfG {
// Returns median of a small group (size <= 5)
static int getMedian(int[] group) {
Array.Sort(group);
return group[group.Length / 2];
}
// Function to Partition array from index
// l to r around the pivot value x
static int partitionAroundPivot(int[] arr, int l, int r, int x) {
// Move pivot x to end
int i;
for (i = l; i < r; i++) {
if (arr[i] == x) break;
}
int temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
// Standard partition logic
i = l;
for (int j = l; j < r; j++) {
if (arr[j] <= x) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
i++;
}
}
int t2 = arr[i];
arr[i] = arr[r];
arr[r] = t2;
// Final position of pivot
return i;
}
// Recursively finds the k-th smallest element in arr[l..r]
static int selectKthSmallest(int[] arr, int l, int r, int k) {
if (k > 0 && k <= r - l + 1) {
int n = r - l + 1;
List<int> medians = new List<int>();
int i;
// Divide array into groups of 5 and store their medians
for (i = 0; i < n / 5; i++) {
int[] group = new int[5];
Array.Copy(arr, l + i * 5, group, 0, 5);
medians.Add(getMedian(group));
}
// Handle the last group with less than 5 elements
if (i * 5 < n) {
int len = n % 5;
int[] lastGroup = new int[len];
Array.Copy(arr, l + i * 5, lastGroup, 0, len);
medians.Add(getMedian(lastGroup));
}
// Find median of medians
int pivot;
if (medians.Count == 1) {
pivot = medians[0];
} else {
int[] medArr = medians.ToArray();
pivot = selectKthSmallest(medArr, 0, medArr.Length - 1, medArr.Length / 2);
}
// Partition array and get position of pivot
int pos = partitionAroundPivot(arr, l, r, pivot);
// If position matches k, return result
if (pos - l == k - 1) return arr[pos];
// Recur on left or right part accordingly
if (pos - l > k - 1)
return selectKthSmallest(arr, l, pos - 1, k);
return selectKthSmallest(arr, pos + 1, r, k - pos + l - 1);
}
return int.MaxValue;
}
// Function to find kth Smallest in Array
static int kthSmallest(int[] arr, int k) {
return selectKthSmallest(arr, 0, arr.Length - 1, k);
}
static void Main() {
int[] arr = {7, 10, 4, 3, 20, 15};
int k = 3;
Console.WriteLine(kthSmallest(arr, k));
}
}
JavaScript
// JavaScript implementation of the Worst Case Linear Time algorithm
// to find the k-th smallest element using Median of Medians
// Returns median of a small group (size <= 5)
function getMedian(group) {
group.sort((a, b) => a - b);
return group[Math.floor(group.length / 2)];
}
// Function to Partition array from index
// l to r around the pivot value x
function partitionAroundPivot(arr, l, r, x) {
// Move pivot x to end
let i;
for (i = l; i < r; i++) {
if (arr[i] === x) break;
}
[arr[i], arr[r]] = [arr[r], arr[i]];
// Standard partition logic
i = l;
for (let j = l; j < r; j++) {
if (arr[j] <= x) {
[arr[i], arr[j]] = [arr[j], arr[i]];
i++;
}
}
[arr[i], arr[r]] = [arr[r], arr[i]];
// Final position of pivot
return i;
}
// Recursively finds the k-th smallest element in arr[l..r]
function selectKthSmallest(arr, l, r, k) {
if (k > 0 && k <= r - l + 1) {
let n = r - l + 1;
let medians = [];
// Divide array into groups of 5 and store their medians
let i;
for (i = 0; i < Math.floor(n / 5); i++) {
let group = arr.slice(l + i * 5, l + i * 5 + 5);
medians.push(getMedian(group));
}
// Handle the last group with less than 5 elements
if (i * 5 < n) {
let lastGroup = arr.slice(l + i * 5, l + i * 5 + (n % 5));
medians.push(getMedian(lastGroup));
}
// Find median of medians
let pivot;
if (medians.length === 1) {
pivot = medians[0];
} else {
pivot = selectKthSmallest(medians, 0, medians.length - 1,
Math.floor(medians.length / 2));
}
// Partition array and get position of pivot
let pos = partitionAroundPivot(arr, l, r, pivot);
// If position matches k, return result
if (pos - l === k - 1) return arr[pos];
// Recur on left or right part accordingly
if (pos - l > k - 1)
return selectKthSmallest(arr, l, pos - 1, k);
return selectKthSmallest(arr, pos + 1, r, k - pos + l - 1);
}
return Infinity;
}
// Function to find kth Smallest in Array
function kthSmallest(arr, k) {
return selectKthSmallest(arr, 0, arr.length - 1, k);
}
// Driver code
let arr = [7, 10, 4, 3, 20, 15];
let k = 3;
console.log(kthSmallest(arr, k));
Time Complexity: O(n), Worst-case linear time for selection
Space Complexity: O(n), Extra space for storing medians recursively in each level of selection calls.
Detailed Time Complexity Analysis
We analyze the worst-case time complexity of the Median of Medians algorithm step by step:
- Dividing the array into groups of 5. There are n/5 such groups. Finding the median of each group takes O(1) time since the size is constant. So, the total time for this step is O(n).
- Finding the median of medians. Recursively finding the median of the n/5 medians takes T(n/5) time.
- Partitioning the array around the pivot (median of medians) using a standard partition operation takes O(n) time.
- After partitioning, the recursive call proceeds into one side (either left or right) depending on where the k-th smallest element lies.
To understand the size of the recursive call, we analyze how many elements are guaranteed to be greater or smaller than the pivot. At least half of the n/5 medians are greater than or equal to the median of medians. Each of those groups contributes at least 3 elements greater than the median of medians. Therefore, the number of elements greater than or equal to the pivot is at least (3 * ceil(1/2 * ceil(n/5)) - 6), which is at least (3n/10 - 6). Similarly, the number of elements smaller than the pivot is at least 3n/10 - 6.
So, in the worst case, the recursive call goes to at most n - (3n/10 - 6) = 7n/10 + 6 elements.
The recurrence relation becomes:
T(n) <= Θ(1), if n <= 80
T(n) <= T(ceil(n/5)) + T(7n/10 + 6) + O(n), if n > 80
We prove T(n) = O(n) by substitution. Assume T(n) <= cn for some constant c and for all n > 80.
Substituting into the recurrence:
T(n) <= c(n/5) + c(7n/10 + 6) + O(n)
= cn/5 + 7cn/10 + 6c + O(n)
= 9cn/10 + 6c + O(n)
We can choose c large enough such that cn/10 >= 6c + O(n), so T(n) <= cn.
Hence, the worst-case running time is linear, O(n).
Conclusion
Although this algorithm is linear in the worst case, the constants involved are large due to recursive overhead and multiple passes. In practice, a randomized QuickSelect is much faster and is generally preferred despite its average-case nature.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem