Sort an array having 3 types of Values
Last Updated :
23 Jul, 2025
We are given an array containing three different types of elements, and the task is to sort the array. This problem has been asked in various forms, and in this article, we will explore the three most common forms of this problem.
Sort an array of 0s, 1s and 2s
Given an array arr[] consisting of only 0s, 1s, and 2s. The task is to sort the array, i.e., put all 0s first, then all 1s and all 2s in last.
This problem is the same as the famous “Dutch National Flag problem” which is proposed by Edsger Dijkstra.
Examples:
Input: arr[] = {0, 1, 2, 0, 1, 2}
Output: {0, 0, 1, 1, 2, 2}
Explanation: {0, 0, 1, 1, 2, 2} has all 0s first, then all 1s and all 2s in last.
Input: arr[] = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}
Output: {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2}
Explanation: {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2} has all 0s first, then all 1s and all 2s in last.
To know about different approaches to solve this problem, please refer: Sort an array of 0s, 1s and 2s
Similar Problem: Sort an array of 0s, 1s, 2s and 3s
Three way partitioning around a pivot element
Given an array arr[] of integers and a value pivot, the task is to partition the array around the pivot such that array is divided in three parts.
- All elements smaller than pivot come first.
- All elements equal to pivot come next.
- All elements greater than pivot appear in the end.
The individual elements of the three parts can appear in any order. Note: We can assume that the pivot will always be present as an element in the array.
Examples:
Input: arr[] = [0, -1, 3, 2, -7, 0, -5, 6], pivot = 0
Output: [-1, -7, -5, 0, 0, 2, 3, 6]
Explanation: All elements smaller than pivot [-1, -7, -5] were arranged before it and all elements larger than pivot [2, 3, 6] were arranged after it. Note that [-1, -5, -7, 0, 0, 3, 2, 6] is also a valid 3 way partition.
Input: arr[] = [2, 8, 6, 9, 8, 5, 2, 1], pivot = 5
Output: [2, 1, 2, 5, 6, 8, 8, 9]
Explanation: All elements smaller than pivot element [2, 1, 2] were arranged before it and elements larger than pivot [6, 8, 8, 9] were arranged after it.
To know about different approaches to solve this problem, please refer: Three way partitioning around a pivot element
Three way partitioning of an array around a given range
Given an array and a range [lowVal, highVal], partition the array around the range such that array is divided in three parts.
- All elements smaller than lowVal come first.
- All elements in range lowVal to highVal come next.
- All elements greater than highVal appear in the end.
The individual elements of the three parts can appear in any order.
Examples:
Input: arr[] = {1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32}, lowVal = 14, highVal = 20
Output: arr[] = {1, 5, 4, 2, 1, 3, 14, 20, 20, 98, 87, 32, 54}
Explanation: all elements which are less than 14 are present in arr[0 ... 5],
all elements which are greater than equal to 14 and less than equal to 20 are present in arr[6 ... 8],
all elements which are greater than 20 are present in arr[9 ... 12].
Input: arr[] = {1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32}, lowVal = 20, highVal = 20
Output: arr[] = {1, 14, 5, 4, 2, 1, 3, 20, 20, 98, 87, 32, 54}
To know about different approaches to solve this problem, please refer: Three way partitioning of an array around a given range
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem