Find k smallest elements in an array
Last Updated :
23 Jul, 2025
Given an array arr[] and an integer k, the task is to find k smallest elements in the given array. Elements in the output array can be in any order.
Examples:
Input: arr[] = [1, 23, 12, 9, 30, 2, 50], k = 3
Output: [1, 2, 9]
Input: arr[] = [11, 5, 12, 9, 44, 17, 2], k = 2
Output: [2, 5]
[Approach - 1] Using Sorting
The idea is to sort the input array in ascending order, so the first k elements in the array will be the k smallest elements.
C++
// C++ program to find k smallest elements in an
// array using sorting
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> kSmallest(vector<int> &arr, int k) {
// sort the given array in ascending order
sort(arr.begin(), arr.end());
// store the first k element in result array
vector<int> res(arr.begin(), arr.begin() + k);
return res;
}
int main() {
vector<int>arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
vector<int> res = kSmallest(arr, k);
for(int ele : res)
cout << ele << " ";
return 0;
}
Java
// Java program to find k smallest elements in an
// array using sorting
import java.util.Arrays;
import java.util.ArrayList;
class GfG {
static ArrayList<Integer> kSmallest(int[] arr, int k) {
// Sort the given array in ascending order
Arrays.sort(arr);
// Store the first k elements in result ArrayList
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < k; i++)
res.add(arr[i]);
return res;
}
public static void main(String[] args) {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
ArrayList<Integer> res = kSmallest(arr, k);
for (int ele : res)
System.out.print(ele + " ");
}
}
Python
# Python program to find k smallest elements in an
# array using sorting
def kSmallest(arr, k):
# Sort the given array in ascending order
arr.sort()
# Store the first k elements in result array
res = arr[:k]
return res
if __name__ == "__main__":
arr = [1, 23, 12, 9, 30, 2, 50]
k = 3
res = kSmallest(arr, k)
for ele in res:
print(ele, end=" ")
C#
// C# program to find k smallest elements in an
// array using sorting
using System;
using System.Collections.Generic;
class GfG {
static List<int> kSmallest(int[] arr, int k) {
// Sort the given array in ascending order
Array.Sort(arr);
// Store the first k elements in result List
List<int> res = new List<int>();
for (int i = 0; i < k; i++)
res.Add(arr[i]);
return res;
}
static void Main() {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
List<int> res = kSmallest(arr, k);
foreach (int ele in res)
Console.Write(ele + " ");
}
}
JavaScript
// JavaScript program to find k smallest elements in an
// array using sorting
function kSmallest(arr, k) {
// Sort the given array in ascending order
arr.sort((a, b) => a - b);
// Store the first k elements in result array
let res = arr.slice(0, k);
return res;
}
const arr = [1, 23, 12, 9, 30, 2, 50];
const k = 3;
const res = kSmallest(arr, k);
console.log(res.join(' '));
Time complexity: O(n * logn)
Auxiliary Space: O(1)
[Approach - 2] Using Quick Sort Partitioning Step (Quick Select Algorithm)
The idea is to use the partitioning step of QuickSort to find the k smallest elements in the array, without sorting the entire array. In the partitioning step, we rearrange the elements in a way that all elements smaller than or equal to a chosen pivot (usually the last element) are placed on its left, and all elements greater than the pivot are on its right. And pivot element in its correct sorted position.
After each partition, we compare the number of elements in the left part of the array (which contains all elements smaller than or equal to the pivot) with k:
- Number of elements in the left = k, it means all elements in the left part (including pivot) are the k smallest elements.
- Number of elements in the left > k, it means that k smallest elements exist in the left subarray only, so we recursively search in the left subarray.
- Number of elements in the left < k, it means that the k smallest elements include the entire left part of the array along with some elements from the right part. Therefore we reduce k by the number of elements already covered on the left side and search in the right subarray.
C++
// C++ program to find the k smallest elements in the array
// using partitioning step of quick sort
#include <iostream>
#include <vector>
using namespace std;
// Function to partition the array around a pivot
int partition(vector<int> &arr, int left, int right) {
// Last element is chosen as a pivot.
int pivot = arr[right];
int i = left;
for (int j = left; j < right; j++) {
if (arr[j] <= pivot) {
swap(arr[i], arr[j]);
i++;
}
}
swap(arr[i], arr[right]);
// The correct sorted position of the pivot
return i;
}
void quickSelect(vector<int> &arr, int left, int right, int k) {
if (left <= right) {
int pivotIdx = partition(arr, left, right);
// Count of all elements in the left part
int leftCnt = pivotIdx - left + 1;
// If leftCnt is equal to k, then we have
// found the k smallest element
if (leftCnt == k)
return;
// Search in the left subarray
if (leftCnt > k)
quickSelect(arr, left, pivotIdx - 1, k);
// Reduce the k by number of elements already covered
// and search in the right subarray
else
quickSelect(arr, pivotIdx + 1, right, k - leftCnt);
}
}
vector<int> kSmallest(vector<int> &arr, int k) {
int n = arr.size();
quickSelect(arr, 0, n - 1, k);
// First k elements of the array, will be the smllest
vector<int> res(arr.begin(), arr.begin() + k);
return res;
}
int main() {
vector<int> arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
vector<int> res = kSmallest(arr, k);
for (int ele : res)
cout << ele << " ";
return 0;
}
Java
// Java program to find the k smallest elements in the array
// using partitioning step of quick sort
import java.util.ArrayList;
class GfG {
// Function to partition the array around a pivot
static int partition(int[] arr, int left, int right) {
// Last element is chosen as a pivot.
int pivot = arr[right];
int i = left;
for (int j = left; j < right; j++) {
if (arr[j] <= pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
int temp = arr[i];
arr[i] = arr[right];
arr[right] = temp;
// The correct sorted position of the pivot
return i;
}
static void quickSelect(int[] arr, int left, int right, int k) {
if (left <= right) {
int pivotIdx = partition(arr, left, right);
// Count of all elements in the left part
int leftCnt = pivotIdx - left + 1;
// If leftCnt is equal to k, then we have
// found the k smallest element
if (leftCnt == k)
return;
// Search in the left subarray
if (leftCnt > k)
quickSelect(arr, left, pivotIdx - 1, k);
// Reduce the k by number of elements already covered
// and search in the right subarray
else
quickSelect(arr, pivotIdx + 1, right, k - leftCnt);
}
}
static ArrayList<Integer> kSmallest(int[] arr, int k) {
quickSelect(arr, 0, arr.length - 1, k);
ArrayList<Integer> res = new ArrayList<>();
// First k elements of the array, will be the smallest
for(int i = 0; i < k; i++)
res.add(arr[i]);
return res;
}
public static void main(String[] args) {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
ArrayList<Integer> res = kSmallest(arr, k);
for (int ele : res)
System.out.print(ele + " ");
}
}
Python
# Python program to find the k smallest elements in the array
# using partitioning step of quick sort
# Function to partition the array around a pivot
def partition(arr, left, right):
# Last element is chosen as a pivot.
pivot = arr[right]
i = left
for j in range(left, right):
if arr[j] <= pivot:
arr[i], arr[j] = arr[j], arr[i]
i += 1
arr[i], arr[right] = arr[right], arr[i]
# The correct sorted position of the pivot
return i
def quickSelect(arr, left, right, k):
if left <= right:
pivotIdx = partition(arr, left, right)
# Count of all elements in the left part
leftCnt = pivotIdx - left + 1
# If leftCnt is equal to k, then we have
# found the k smallest element
if leftCnt == k:
return
# Search in the left subarray
if leftCnt > k:
quickSelect(arr, left, pivotIdx - 1, k)
# Reduce the k by number of elements already covered
# and search in the right subarray
else:
quickSelect(arr, pivotIdx + 1, right, k - leftCnt)
def kSmallest(arr, k):
quickSelect(arr, 0, len(arr) - 1, k)
# First k elements of the array, will be the smallest
return arr[:k]
if __name__ == "__main__":
arr = [1, 23, 12, 9, 30, 2, 50]
k = 3
res = kSmallest(arr, k)
print(" ".join(map(str, res)))
C#
// C# program to find the k smallest elements in the array
// using partitioning step of quick sort
using System;
using System.Collections.Generic;
class GfG {
// Function to partition the array around a pivot
static int partition(int[] arr, int left, int right) {
// Last element is chosen as a pivot.
int pivot = arr[right];
int i = left;
for (int j = left; j < right; j++) {
if (arr[j] <= pivot) {
swap(arr, i, j);
i++;
}
}
swap(arr, i, right);
// The correct sorted position of the pivot
return i;
}
static void quickSelect(int[] arr, int left, int right, int k) {
if (left <= right) {
int pivotIdx = partition(arr, left, right);
// Count of all elements in the left part
int leftCnt = pivotIdx - left + 1;
// If leftCnt is equal to k, then we have
// found the k smallest element
if (leftCnt == k)
return;
// Search in the left subarray
if (leftCnt > k)
quickSelect(arr, left, pivotIdx - 1, k);
// Reduce the k by number of elements already covered
// and search in the right subarray
else
quickSelect(arr, pivotIdx + 1, right, k - leftCnt);
}
}
static List<int> kSmallest(int[] arr, int k) {
quickSelect(arr, 0, arr.Length - 1, k);
// First k elements of the array, will be the smallest
List<int> res = new List<int>();
for (int i = 0; i < k; i++) {
res.Add(arr[i]);
}
return res;
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void Main() {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
List<int> res = kSmallest(arr, k);
foreach (int ele in res)
Console.Write(ele + " ");
}
}
JavaScript
// JavaScript program to find the k smallest elements in the array
// using partitioning step of quick sort
// Function to partition the array around a pivot
function partition(arr, left, right) {
// Last element is chosen as a pivot.
let pivot = arr[right];
let i = left;
for (let j = left; j < right; j++) {
if (arr[j] <= pivot) {
[arr[i], arr[j]] = [arr[j], arr[i]];
i++;
}
}
[arr[i], arr[right]] = [arr[right], arr[i]];
// The correct sorted position of the pivot
return i;
}
function quickSelect(arr, left, right, k) {
if (left <= right) {
let pivotIdx = partition(arr, left, right);
// Count of all elements in the left part
let leftCnt = pivotIdx - left + 1;
// If leftCnt is equal to k, then the first
// k element of the array will be smallest
if (leftCnt === k)
return;
// Search in the left subarray
if (leftCnt > k)
quickSelect(arr, left, pivotIdx - 1, k);
// Reduce the k by number of elements already covered
// and search in the right subarray
else
quickSelect(arr, pivotIdx + 1, right, k - leftCnt);
}
}
function kSmallest(arr, k) {
quickSelect(arr, 0, arr.length - 1, k);
// First k elements of the array, will be the smallest
return arr.slice(0, k);
}
const arr = [1, 23, 12, 9, 30, 2, 50];
const k = 3;
const res = kSmallest(arr, k);
console.log(res.join(' '));
Time Complexity: O(n*n) in worst case(O(n) on average)
Auxiliary Space: O(n)
[Approach - 3] Using Priority Queue(Max-Heap)
The idea is, as we iterate through the array, we keep track of the k smallest elements at each step. To achieve this, we use a max-heap. First, we insert the initial k elements into the max-heap. After that, for each next element, we compare it with the top of the heap. Since the top element of the max-heap is the largest among the k elements, if the current element is smaller than the top, it means the top element is no longer one of the k smallest elements. In this case, we remove the top and insert the smaller element. After completing the entire traversal, the heap will contain exactly the k smallest elements of the array.
C++
// C++ program to find the k smallest elements in the
// array using max heap
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> kSmallest(vector<int> &arr, int k) {
// Max Priority Queue (Max-Heap)
priority_queue<int> maxH;
for (int i = 0; i < arr.size(); i++) {
// Insert initial k elements in the max heap
if (maxH.size() < k)
maxH.push(arr[i]);
// If the top of heap is greater than the arr[i]
// then remove this and insert arr[i]
else if(maxH.top() > arr[i]) {
maxH.pop();
maxH.push(arr[i]);
}
}
vector<int> res;
// Max heap will contain only k
// smallest element
while (!maxH.empty()) {
res.push_back(maxH.top());
maxH.pop();
}
return res;
}
int main() {
vector<int> arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
vector<int> res = kSmallest(arr, k);
for(int ele : res)
cout << ele << " ";
return 0;
}
Java
// Java program to find the k smallest elements in the array
// using max heap
import java.util.PriorityQueue;
import java.util.ArrayList;
import java.util.Collections;
class GfG {
static ArrayList<Integer> kSmallest(int[] arr, int k) {
// Max Priority Queue (Max-Heap)
PriorityQueue<Integer> maxH =
new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < arr.length; i++) {
// Insert initial k elements in the max heap
if (maxH.size() < k)
maxH.add(arr[i]);
// If the top of heap is greater than arr[i]
// then remove this and insert arr[i]
else if (maxH.peek() > arr[i]) {
maxH.poll();
maxH.add(arr[i]);
}
}
// Max heap will contain all k smallest elements
ArrayList<Integer> res = new ArrayList<>();
while (!maxH.isEmpty()) {
res.add(maxH.poll());
}
return res;
}
public static void main(String[] args) {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
ArrayList<Integer> res = kSmallest(arr, k);
for (int ele : res) {
System.out.print(ele + " ");
}
}
}
Python
# Python program to find the k smallest elements in the
# array using max heap
import heapq
def kSmallest(arr, k):
# Max Priority Queue (Max-Heap)
maxH = []
for num in arr:
# Insert initial k elements in the max heap
if len(maxH) < k:
heapq.heappush(maxH, -num)
# If the top of heap is greater than
# arr[i], remove this and insert arr[i]
elif -maxH[0] > num:
heapq.heappop(maxH)
heapq.heappush(maxH, -num)
# Max heap will contain all k smallest elements
res = [-x for x in maxH]
return res
if __name__ == "__main__":
arr = [1, 23, 12, 9, 30, 2, 50]
k = 3
res = kSmallest(arr, k)
for ele in res:
print(ele, end=" ")
C#
// C# program to find the k smallest elements in the
// array using max heap
using System;
using System.Collections.Generic;
class GfG {
static List<int> kSmallest(int[] arr, int k) {
// Max Priority Queue (Max-Heap)
SortedSet<int> maxH =
new SortedSet<int>(Comparer<int>.Create((a, b) => b.CompareTo(a)));
for (int i = 0; i < arr.Length; i++) {
// Insert initial k elements in the max heap
if (maxH.Count < k)
maxH.Add(arr[i]);
// If the top of heap is greater than arr[i]
// then remove this and insert arr[i]
else if (maxH.Min > arr[i]) {
maxH.Remove(maxH.Min);
maxH.Add(arr[i]);
}
}
// Max heap will contain all k smallest elements
List<int> res = new List<int>(maxH);
return res;
}
static void Main() {
int[] arr = {1, 23, 12, 9, 30, 2, 50};
int k = 3;
List<int> res = kSmallest(arr, k);
foreach (int ele in res)
Console.Write(ele + " ");
}
}
JavaScript
// JavaScript program to find the k smallest elements in the
// array using max heap
function kSmallest(arr, k) {
// Max Priority Queue (Max-Heap)
let maxH = [];
for (let i = 0; i < arr.length; i++) {
// Insert initial k elements in the max heap
if (maxH.length < k) {
maxH.push(arr[i]);
maxH.sort((a, b) => b - a);
}
// If the top of heap is greater than arr[i]
// then remove this and insert arr[i]
else if (maxH[0] > arr[i]) {
maxH.shift();
maxH.push(arr[i]);
maxH.sort((a, b) => b - a);
}
}
// Max heap will contain only k smallest elements
return maxH;
}
const arr = [1, 23, 12, 9, 30, 2, 50];
const k = 3;
const res = kSmallest(arr, k);
console.log(res.join(' '));
Time Complexity: O(n * log(k))
Auxiliary Space: O(k)
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