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 3Input: 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++
// C++ code for the approach #include <bits/stdc++.h> using namespace std; // Driver code int main() { // Input array int arr[] = { 3, 2, 1, 0, 2, 3, 1, 0 }; int size = sizeof (arr) / sizeof (arr[0]); // inbuilt sort function to sort the array sort(arr, arr + size); // printing the sorted array for ( int i = 0; i < size; i++) { cout << arr[i] << " " ; } cout << endl; return 0; } |
Java
// Java code implementation import java.util.Arrays; public class Main { // Driver code public static void main(String[] args) { // Input array int [] arr = { 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 }; int size = arr.length; // inbuilt sort function to sort the array Arrays.sort(arr); // printing the sorted array for ( int i = 0 ; i < size; i++) { System.out.print(arr[i] + " " ); } System.out.println(); } } |
Python3
# Input array arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ] # Inbuilt sort function to sort the array arr.sort() # Printing the sorted array for i in arr: print (i, end = " " ) print () |
C#
// C# code for the approach using System; class Program { static void Main() { // Input array int [] arr = { 3, 2, 1, 0, 2, 3, 1, 0 }; int size = arr.Length; // inbuilt sort function to sort the array Array.Sort(arr); // printing the sorted array for ( int i = 0; i < size; i++) { Console.Write(arr[i] + " " ); } Console.WriteLine(); } } // This code is contributed by sarojmcy2e |
Javascript
// JavaScript code for the approach // Input array let arr = [3, 2, 1, 0, 2, 3, 1, 0]; // inbuilt sort function to sort the array arr.sort(); // printing the sorted array for (let i = 0; i < arr.length; i++) { console.log(arr[i] + " " ); } console.log(); |
0 0 1 1 2 2 3 3
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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to sort the array having // array element only 0, 1, 2, and 3 void sortArray( int arr[], int N) { int i = 0, j = N - 1, mid = 0; // Iterate until mid <= j while (mid <= j) { // If arr[mid] is 0 if (arr[mid] == 0) { // Swap integers at // indices i and mid swap(arr[i], arr[mid]); // Increment i i++; // Increment mid mid++; } // Otherwise if the value of // arr[mid] is 3 else if (arr[mid] == 3) { // Swap arr[mid] and arr[j] swap(arr[mid], arr[j]); // Decrement j j--; } // Otherwise if the value of // arr[mid] is either 1 or 2 else if (arr[mid] == 1 || arr[mid] == 2) { // Increment the value of mid mid++; } } // Iterate until i <= j while (i <= j) { // If arr[i] the value of is 2 if (arr[i] == 2) { // Swap arr[i] and arr[j] swap(arr[i], arr[j]); // Decrement j j--; } // Otherwise, increment i else { i++; } } // Print the sorted array arr[] for ( int i = 0; i < N; i++) { cout << arr[i] << " " ; } } // Driver Code 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
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG{ // Function to sort the array having // array element only 0, 1, 2, and 3 static void sortArray( int [] arr, int N) { int i = 0 , j = N - 1 , mid = 0 ; // Iterate until mid <= j while (mid <= j) { // If arr[mid] is 0 if (arr[mid] == 0 ) { // Swap integers at // indices i and mid int temp = arr[i]; arr[i] = arr[mid]; arr[mid] = temp; // Increment i i++; // Increment mid mid++; } // Otherwise if the value of // arr[mid] is 3 else if (arr[mid] == 3 ) { // Swap arr[mid] and arr[j] int temp = arr[mid]; arr[mid] = arr[j]; arr[j] = temp; // Decrement j j--; } // Otherwise if the value of // arr[mid] is either 1 or 2 else if (arr[mid] == 1 || arr[mid] == 2 ) { // Increment the value of mid mid++; } } // Iterate until i <= j while (i <= j) { // If arr[i] the value of is 2 if (arr[i] == 2 ) { // Swap arr[i] and arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; // Decrement j j--; } // Otherwise, increment i else { i++; } } // Print the sorted array arr[] for ( int k = 0 ; k < N; k++) { System.out.print(arr[k] + " " ); } } // Driver Code public static void main(String[] args) { int [] arr = { 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 }; int N = arr.length; sortArray(arr, N); } } // This code is contributed by susmitakundugoaldanga |
Python3
# Python3 program for the above approach # Function to sort the array having # array element only 0, 1, 2, and 3 def sortArray(arr, N): i = 0 j = N - 1 mid = 0 # Iterate until mid <= j while (mid < = j): # If arr[mid] is 0 if (arr[mid] = = 0 ): # Swap integers at # indices i and mid arr[i], arr[mid] = arr[mid], arr[i] # Increment i i + = 1 # Increment mid mid + = 1 # Otherwise if the value of # arr[mid] is 3 elif (arr[mid] = = 3 ): # Swap arr[mid] and arr[j] arr[mid], arr[j] = arr[j], arr[mid] # Decrement j j - = 1 # Otherwise if the value of # arr[mid] is either 1 or 2 elif (arr[mid] = = 1 or arr[mid] = = 2 ): # Increment the value of mid mid + = 1 # Iterate until i <= j while (i < = j): # If arr[i] the value of is 2 if (arr[i] = = 2 ): # Swap arr[i] and arr[j] arr[i], arr[j] = arr[j], arr[i] # Decrement j j - = 1 # Otherwise, increment i else : i + = 1 # Print the sorted array arr[] for i in range (N): print (arr[i], end = " " ) # Driver Code if __name__ = = '__main__' : arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ] N = len (arr) sortArray(arr, N) # This code is contributed by mohit kumar 29. |
C#
// C# program for the above approach using System; class GFG{ // Function to sort the array having // array element only 0, 1, 2, and 3 static void sortArray( int [] arr, int N) { int i = 0, j = N - 1, mid = 0; // Iterate until mid <= j while (mid <= j) { // If arr[mid] is 0 if (arr[mid] == 0) { // Swap integers at // indices i and mid int temp = arr[i]; arr[i] = arr[mid]; arr[mid] = temp; // Increment i i++; // Increment mid mid++; } // Otherwise if the value of // arr[mid] is 3 else if (arr[mid] == 3) { // Swap arr[mid] and arr[j] int temp = arr[mid]; arr[mid] = arr[j]; arr[j] = temp; // Decrement j j--; } // Otherwise if the value of // arr[mid] is either 1 or 2 else if (arr[mid] == 1 || arr[mid] == 2) { // Increment the value of mid mid++; } } // Iterate until i <= j while (i <= j) { // If arr[i] the value of is 2 if (arr[i] == 2) { // Swap arr[i] and arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; // Decrement j j--; } // Otherwise, increment i else { i++; } } // Print the sorted array arr[] for ( int k = 0; k < N; k++) { Console.Write(arr[k] + " " ); } } // Driver Code public static void Main() { int [] arr = { 3, 2, 1, 0, 2, 3, 1, 0 }; int N = arr.Length; sortArray(arr, N); } } // This code is contributed by ukasp |
Javascript
<script> // javascript program for the above approach // Function to sort the array having // array element only 0, 1, 2, and 3 function sortArray(arr, N) { var i = 0, j = N - 1, mid = 0; // Iterate until mid <= j while (mid <= j) { // If arr[mid] is 0 if (arr[mid] == 0) { // Swap integers at // indices i and mid var temp = arr[i]; arr[i] = arr[mid]; arr[mid] = temp; // Increment i i++; // Increment mid mid++; } // Otherwise if the value of // arr[mid] is 3 else if (arr[mid] == 3) { var temp = arr[mid]; arr[mid] = arr[j]; arr[j] = temp; // Decrement j j--; } // Otherwise if the value of // arr[mid] is either 1 or 2 else if (arr[mid] == 1 || arr[mid] == 2) { // Increment the value of mid mid++; } } // Iterate until i <= j while (i <= j) { // If arr[i] the value of is 2 if (arr[i] == 2) { // Swap arr[i] and arr[j] var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; // Decrement j j--; } // Otherwise, increment i else { i++; } } // Print the sorted array arr[] for (i = 0; i < N; i++) { document.write(arr[i] + " " ); } } // Driver Code var arr = [3, 2, 1, 0, 2, 3, 1, 0]; var N = arr.length; sortArray(arr, N); </script> |
0 0 1 1 2 2 3 3
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++
// C++ Program for the above approach #include<bits/stdc++.h> using namespace std; // function to sor the array having // array element only 0,1,2 and 3 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++; } // Putting the 0's in the array in starting. for ( int i = 0; i < count0; i++) arr[i] = 0; // Putting the 1's in the array after the 0's. for ( int i = count0; i < (count0 + count1); i++) arr[i] = 1; // Putting the 2's in the array after the 1's for ( int i = (count0 + count1); i < (count0 + count1 + count2); i++) arr[i] = 2; // Putting the 3's in the array after the 2's for ( int i = (count0 + count1 + count2); i < N; i++) arr[i] = 3; // print the sorted array for ( int i = 0; i<N; i++){ cout<<arr[i]<< " " ; } } // Driver Code int main() { int arr[] = { 3, 2, 1, 0, 2, 3, 1, 0 }; int N = sizeof (arr) / sizeof (arr[0]); sortArray(arr, N); return 0; } // THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGARWAL2852002) |
Java
import java.util.Arrays; class Main { // function to sor the array having // array element only 0,1,2 and 3 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++; } // Putting the 0's in the array in starting. for ( int i = 0 ; i < count0; i++) arr[i] = 0 ; // Putting the 1's in the array after the 0's. for ( int i = count0; i < (count0 + count1); i++) arr[i] = 1 ; // Putting the 2's in the array after the 1's for ( int i = (count0 + count1); i < (count0 + count1 + count2); i++) arr[i] = 2 ; // Putting the 3's in the array after the 2's for ( int i = (count0 + count1 + count2); i < N; i++) arr[i] = 3 ; // print the sorted array System.out.println(Arrays.toString(arr)); } // Driver Code public static void main(String[] args) { int arr[] = { 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 }; int N = arr.length; sortArray(arr, N); } } |
Python3
# Python Program for the above approach # function to sort the array having # array elements only 0,1,2 and 3 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 # Putting the 0's in the array in starting. for i in range (count0): arr[i] = 0 # Putting the 1's in the array after the 0's. for i in range (count0, count0 + count1): arr[i] = 1 # Putting the 2's in the array after the 1's for i in range (count0 + count1, count0 + count1 + count2): arr[i] = 2 # Putting the 3's in the array after the 2's for i in range (count0 + count1 + count2, N): arr[i] = 3 # print the sorted array for i in range (N): print (arr[i], end = " " ) # driver code to test above function arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ] N = len (arr) sortArray(arr, N) |
Javascript
// JavaScript Program for the above approach // function to sort the array having // array elements only 0,1,2 and 3 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++; } // Putting the 0's in the array in starting. for (let i = 0; i < count0; i++) arr[i] = 0; // Putting the 1's in the array after the 0's. for (let i = count0; i < (count0 + count1); i++) arr[i] = 1; // Putting the 2's in the array after the 1's for (let i = (count0 + count1); i < (count0 + count1 + count2); i++) arr[i] = 2; // Putting the 3's in the array after the 2's for (let i = (count0 + count1 + count2); i < N; i++) arr[i] = 3; // print the sorted array for (let i = 0; i<N; i++){ console.log(arr[i] + " " ); } } // driver code to test above function let arr = [3, 2, 1, 0, 2, 3, 1, 0]; let N = arr.length; sortArray(arr, N); // THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL(KIRTIAGARWAL23121999) |
C#
using System; class MainClass { // function to sort the array having // array element only 0, 1, 2, and 3 static void SortArray( int [] arr, int N) { int count0 = 0; int count1 = 0; int count2 = 0; int count3 = 0; // count the occurrence of each element in the array 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++; } // Put the 0's in the array at the beginning. for ( int i = 0; i < count0; i++) arr[i] = 0; // Put the 1's in the array after the 0's. for ( int i = count0; i < (count0 + count1); i++) arr[i] = 1; // Put the 2's in the array after the 1's. for ( int i = (count0 + count1); i < (count0 + count1 + count2); i++) arr[i] = 2; // Put the 3's in the array after the 2's. for ( int i = (count0 + count1 + count2); i < N; i++) arr[i] = 3; // print the sorted array 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); } } |
0 0 1 1 2 2 3 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!