Sort an array of 0s, 1s, 2s and 3s
Given an array arr[] of size N consisting of 0, 1, 2, and 3 only, the task is to sort the given array in ascending order.
Example:
Input: arr[] = {0, 3, 1, 2, 0, 3, 1, 2}
Output: 0 0 1 1 2 2 3 3
Input: arr[] = {0, 1, 3, 1, 0, 1, 3, 2, 1, 2, 0, 3, 0, 0, 1}
Output: 0 0 0 0 0 1 1 1 1 1 2 2 3 3 3
Naive Approach:
Here is the algorithm for the given code to sort an array of size N consisting of 0, 1, 2, and 3 only in ascending order:
- Take an array of integers arr[] of size N consisting of 0, 1, 2, and 3 only.
- Sort the array arr[] in ascending order using the inbuilt sort() function.
- Print the sorted array arr[] as the output.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = { 3, 2, 1, 0, 2, 3, 1, 0 };
int size = sizeof (arr) / sizeof (arr[0]);
sort(arr, arr + size);
for ( int i = 0; i < size; i++) {
cout << arr[i] << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Main
{
public static void main(String[] args)
{
int [] arr = { 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 };
int size = arr.length;
Arrays.sort(arr);
for ( int i = 0 ; i < size; i++) {
System.out.print(arr[i] + " " );
}
System.out.println();
}
}
|
Python3
arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ]
arr.sort()
for i in arr:
print (i, end = " " )
print ()
|
C#
using System;
class Program {
static void Main()
{
int [] arr = { 3, 2, 1, 0, 2, 3, 1, 0 };
int size = arr.Length;
Array.Sort(arr);
for ( int i = 0; i < size; i++) {
Console.Write(arr[i] + " " );
}
Console.WriteLine();
}
}
|
Javascript
let arr = [3, 2, 1, 0, 2, 3, 1, 0];
arr.sort();
for (let i = 0; i < arr.length; i++) {
console.log(arr[i] + " " );
}
console.log();
|
Time Complexity: O(n*logn) as sort() takes n*logn time where n is size of the array.
Auxiliary Space: O(1) as no extra space has been used.
Approach: The given problem can be solved based on the approach discussed in this article. The idea is first to position all the 0s and 3s at the beginning and end of the array, followed by sorting the occurrences of integers 1 and 2.
Follow the steps below to solve the problem:
- Initialize three variables, say i, mid, and j. Set the values of i and mid as 0 and j as (N – 1).
- Iterate a loop until mid ≤ j and perform the following steps:
- If the value of arr[mid] is 0, then swap arr[i] and arr[mid] and increment the value of i and mid by 1.
- Otherwise, if the value of arr[mid] is 3, then swap arr[mid] and arr[j] and decrement j by 1.
- Otherwise, if the value of arr[i] is either 1 or 2, then increment the value of mid by 1.
- Now to sort the subarray over the range [i, j] by iterating until i ≤ j and perform the following operations:
- If the value of arr[i] is 2, then swap arr[i] and arr[j] and decrement the value of j by 1.
- Otherwise, increment the value of i by 1.
- After completing the above steps, print the array arr[] as the resultant sorted array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortArray( int arr[], int N)
{
int i = 0, j = N - 1, mid = 0;
while (mid <= j) {
if (arr[mid] == 0) {
swap(arr[i], arr[mid]);
i++;
mid++;
}
else if (arr[mid] == 3) {
swap(arr[mid], arr[j]);
j--;
}
else if (arr[mid] == 1
|| arr[mid] == 2) {
mid++;
}
}
while (i <= j) {
if (arr[i] == 2) {
swap(arr[i], arr[j]);
j--;
}
else {
i++;
}
}
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 3, 2, 1, 0, 2, 3, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
sortArray(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void sortArray( int [] arr, int N)
{
int i = 0 , j = N - 1 , mid = 0 ;
while (mid <= j)
{
if (arr[mid] == 0 )
{
int temp = arr[i];
arr[i] = arr[mid];
arr[mid] = temp;
i++;
mid++;
}
else if (arr[mid] == 3 )
{
int temp = arr[mid];
arr[mid] = arr[j];
arr[j] = temp;
j--;
}
else if (arr[mid] == 1 || arr[mid] == 2 )
{
mid++;
}
}
while (i <= j)
{
if (arr[i] == 2 )
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;
}
else
{
i++;
}
}
for ( int k = 0 ; k < N; k++)
{
System.out.print(arr[k] + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 };
int N = arr.length;
sortArray(arr, N);
}
}
|
Python3
def sortArray(arr, N):
i = 0
j = N - 1
mid = 0
while (mid < = j):
if (arr[mid] = = 0 ):
arr[i], arr[mid] = arr[mid], arr[i]
i + = 1
mid + = 1
elif (arr[mid] = = 3 ):
arr[mid], arr[j] = arr[j], arr[mid]
j - = 1
elif (arr[mid] = = 1 or arr[mid] = = 2 ):
mid + = 1
while (i < = j):
if (arr[i] = = 2 ):
arr[i], arr[j] = arr[j], arr[i]
j - = 1
else :
i + = 1
for i in range (N):
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ]
N = len (arr)
sortArray(arr, N)
|
C#
using System;
class GFG{
static void sortArray( int [] arr, int N)
{
int i = 0, j = N - 1, mid = 0;
while (mid <= j)
{
if (arr[mid] == 0)
{
int temp = arr[i];
arr[i] = arr[mid];
arr[mid] = temp;
i++;
mid++;
}
else if (arr[mid] == 3)
{
int temp = arr[mid];
arr[mid] = arr[j];
arr[j] = temp;
j--;
}
else if (arr[mid] == 1 || arr[mid] == 2)
{
mid++;
}
}
while (i <= j)
{
if (arr[i] == 2)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;
}
else
{
i++;
}
}
for ( int k = 0; k < N; k++)
{
Console.Write(arr[k] + " " );
}
}
public static void Main()
{
int [] arr = { 3, 2, 1, 0, 2, 3, 1, 0 };
int N = arr.Length;
sortArray(arr, N);
}
}
|
Javascript
<script>
function sortArray(arr, N)
{
var i = 0, j = N - 1, mid = 0;
while (mid <= j) {
if (arr[mid] == 0) {
var temp = arr[i];
arr[i] = arr[mid];
arr[mid] = temp;
i++;
mid++;
}
else if (arr[mid] == 3) {
var temp = arr[mid];
arr[mid] = arr[j];
arr[j] = temp;
j--;
}
else if (arr[mid] == 1
|| arr[mid] == 2) {
mid++;
}
}
while (i <= j) {
if (arr[i] == 2) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;
}
else {
i++;
}
}
for (i = 0; i < N; i++) {
document.write(arr[i] + " " );
}
}
var arr = [3, 2, 1, 0, 2, 3, 1, 0];
var N = arr.length;
sortArray(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach(by simply counting 0,1,2 and 3):
We simply traverse the full array and count the occurrence of 0,1,2 and 3’s.
After traversing the whole array we modify out array and set 0,1,2 and 3’s to correct positions.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void sortArray( int arr[], int N){
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
for ( int i = 0; i<N; i++){
if (arr[i] == 0) count0++;
else if (arr[i] == 1) count1++;
else if (arr[i] == 2) count2++;
else count3++;
}
for ( int i = 0; i < count0; i++)
arr[i] = 0;
for ( int i = count0; i < (count0 + count1); i++)
arr[i] = 1;
for ( int i = (count0 + count1); i < (count0 + count1 + count2); i++)
arr[i] = 2;
for ( int i = (count0 + count1 + count2); i < N; i++)
arr[i] = 3;
for ( int i = 0; i<N; i++){
cout<<arr[i]<< " " ;
}
}
int main()
{
int arr[] = { 3, 2, 1, 0, 2, 3, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
sortArray(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
class Main
{
static void sortArray( int [] arr, int N) {
int count0 = 0 ;
int count1 = 0 ;
int count2 = 0 ;
int count3 = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] == 0 ) count0++;
else if (arr[i] == 1 ) count1++;
else if (arr[i] == 2 ) count2++;
else count3++;
}
for ( int i = 0 ; i < count0; i++)
arr[i] = 0 ;
for ( int i = count0; i < (count0 + count1); i++)
arr[i] = 1 ;
for ( int i = (count0 + count1); i < (count0 + count1 + count2); i++)
arr[i] = 2 ;
for ( int i = (count0 + count1 + count2); i < N; i++)
arr[i] = 3 ;
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
int arr[] = { 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 };
int N = arr.length;
sortArray(arr, N);
}
}
|
Python3
def sortArray(arr, N):
count0 = 0
count1 = 0
count2 = 0
count3 = 0
for i in range (N):
if (arr[i] = = 0 ):
count0 + = 1
elif (arr[i] = = 1 ):
count1 + = 1
elif (arr[i] = = 2 ):
count2 + = 1
else :
count3 + = 1
for i in range (count0):
arr[i] = 0
for i in range (count0, count0 + count1):
arr[i] = 1
for i in range (count0 + count1, count0 + count1 + count2):
arr[i] = 2
for i in range (count0 + count1 + count2, N):
arr[i] = 3
for i in range (N):
print (arr[i], end = " " )
arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ]
N = len (arr)
sortArray(arr, N)
|
Javascript
function sortArray(arr, N){
let count0 = 0;
let count1 = 0;
let count2 = 0;
let count3 = 0;
for (let i = 0; i<N; i++){
if (arr[i] == 0) count0++;
else if (arr[i] == 1) count1++;
else if (arr[i] == 2) count2++;
else count3++;
}
for (let i = 0; i < count0; i++)
arr[i] = 0;
for (let i = count0; i < (count0 + count1); i++)
arr[i] = 1;
for (let i = (count0 + count1); i < (count0 + count1 + count2); i++)
arr[i] = 2;
for (let i = (count0 + count1 + count2); i < N; i++)
arr[i] = 3;
for (let i = 0; i<N; i++){
console.log(arr[i] + " " );
}
}
let arr = [3, 2, 1, 0, 2, 3, 1, 0];
let N = arr.length;
sortArray(arr, N);
|
C#
using System;
class MainClass
{
static void SortArray( int [] arr, int N)
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] == 0)
count0++;
else if (arr[i] == 1)
count1++;
else if (arr[i] == 2)
count2++;
else
count3++;
}
for ( int i = 0; i < count0; i++)
arr[i] = 0;
for ( int i = count0; i < (count0 + count1); i++)
arr[i] = 1;
for ( int i = (count0 + count1); i < (count0 + count1 + count2); i++)
arr[i] = 2;
for ( int i = (count0 + count1 + count2); i < N; i++)
arr[i] = 3;
Console.WriteLine( string .Join( ", " , arr));
}
static void Main( string [] args)
{
int [] arr = { 3, 2, 1, 0, 2, 3, 1, 0 };
int N = arr.Length;
SortArray(arr, N);
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
26 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...