Sort an array of 0s, 1s and 2s – Simple Counting Sort Solution

Sort an array

Sort an array approach :

Programming fundamentals include sorting an array, and Java provides a variety of techniques for doing so. We’ll look at how to sort an array in Java in this blog.

When you sort an array, you put the items in a particular order, such as ascending or descending. This can be useful in a lot of situations, such as when you need to quickly locate a certain element in the array or when you want to display the items in a particular order.

The sort() method of the Arrays class is one of the built-in sorting methods offered by Java for arrays. An array of primitive data types, such as int, long, and double, are sorted using the Dual-Pivot Quicksort algorithm via the sort() method, while an array of objects is sorted using the merge sort algorithm.

Example :

Here’s an example of how to sort an array of integers in ascending order using the sort() method of the Arrays class:

import java.util.Arrays;

int[] arr = {3, 60, 35, 2, 45, 320, 5};

Arrays.sort(arr);

for (int i : arr) {
    System.out.print(i + " ");
}

In the above code, we first import the Arrays class, then we create an array of integers called arr. Next, we call the sort() method of the Arrays class on the array and finally, we use a for-each loop to print the sorted array.

Question from GeeksForGeeks :

Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.

Example 1:

Input: 
N = 5
arr[]= {0 2 1 2 0}
Output:
0 0 1 2 2
Explanation:
0s 1s and 2s are segregated 
into ascending order.

Example 2:

Input: 
N = 3
arr[] = {0 1 0}
Output:
0 0 1

Explanation:
0s 1s and 2s are segregated 
into ascending order.

Your Task:
You don't need to read input or print anything. Your task is to complete the function sort012() that takes an array arr and N as input parameters and sorts the array in-place.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)


Constraints:
1 <= N <= 10^6
0 <= A[i] <= 2

Solution using predefined function :

//Solution by technoname.com

class Solution
{
    public static void sort012(int a[], int n)
    {
       Arrays.sort(a);
    }
}

Solution using Bubble sort algorithm :

class Solution
{
    public static void sort012(int a[], int n)
    {
        int i ,j,temp=0;
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                if(a[i]<a[j]){
                    temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
            }
        }
    }
}

Thank you !!

By reading this article, you can enhance your knowledge and develop your programming skills. We’ve provided insightful suggestions and specific examples for sorting an array in Java.

Please feel free to share any other approaches or methods you may have with us by posting a comment or emailing technonamecontact@gmail.com. Your code can be highlighted on our website.

To further expand your understanding of programming concepts, check out our article on the most frequently asked questions about strings, complete with examples. This will give you a deeper understanding of string operations and increase your chances of success in an interview.

If you found this article informative and helpful, please share it with your friends and colleagues. If you have any questions or feedback, please feel free to leave a comment or contact us.

Follow us on Twitter for daily updates and new content.

Leave a Reply

Your email address will not be published. Required fields are marked *