Sort an array of 0s, 1s and 2s

 Problem statement:- Program to sort an array of 0s, 1s, and 2s.

Example:-

              Input : Given

                                 size=8

                                 arr[]=[1,0,2,1,0,2,0,1] 

             Output:  After segregate 0s, 1s and 2s in an Array, Array is:0 0 0 1 1 1 2 2                

Data requirement:-

   Input Data:- size, arr

  Output Data:-arr

  Additional Data:- i,j,temp

Program in C

Here is the source code of the C Program to sort an array of 0s, 1s, and 2s.

Code:

#include<stdio.h>
main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size];
    int i,j;
    printf("Enter the Element of the array(only 0s, 1s, and 2s):\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }
    int temp;
      for(i=0;i<size;i++)
    {
        for(j=i+1;j<size;j++)
        if(arr[i]>=arr[j])
        {
         temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
        }}
    printf("After segregate 0s, 1s and 2s in an Array, Array is:");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
}

Input/Output:
Enter the size of the array:8
Enter the Element of the array(only 0s, 1s, and 2s):
1 0 2 1 0 2 0 1
After segregate 0s, 1s and 2s in an Array, Array is:0 0 0 1 1 1 2 2

Program in C++

Here is the source code of the C++ Program to sort an array of 0s, 1s, and 2s.

Code:

#include<iostream>
using namespace std;
int main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size];
    int i,j;
    cout<<"Enter the Element of the array(only 0s, 1s, and 2s):\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }
    int temp;
      for(i=0;i<size;i++)
    {
        for(j=i+1;j<size;j++)
        if(arr[i]>=arr[j])
        {
         temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
        }}
    cout<<"After segregate 0s, 1s and 2s in an Array, Array is:";
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array(only 0s, 1s, and 2s):
0 1 2 0 2
After segregate 0s, 1s and 2s in an Array, Array is:0 0 1 2 2

Program in Java

Here is the source code of the Java Program to sort an array of 0s, 1s, and 2s.

Code:

import java.util.Scanner;
public class Sort0s1sAnd2s {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
     int size,i,j;
     size=sc.nextInt();
     int arr[ ]=new int[size];
     
  System.out.println("Enter the Element of the array(only 0s, 1s, and 2s):");
      for(i=0;i<size;i++)
     {
         arr[i]=sc.nextInt();
     }
      int temp;
      for(i=0;i<size;i++)
    {
        for(j=i+1;j<size;j++)
        if(arr[i]>=arr[j])
        {
         temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
        }}
      System.out.print("After segregate 0s, 1s and 2s in an Array, Array is:");
    for(i=0;i<size;i++)
    {
    System.out.print(arr[i]+" ");
    } 
      
   sc.close();
}
}

Input/Output:
Enter the size of the array:
6
Enter the Element of the array(only 0s, 1s, and 2s):
2 1 0 0 1 2
After segregate 0s, 1s and 2s in an Array, Array is:0 0 1 1 2 2 

Program in Python

Here is the source code of the Python Program to sort an array of 0s, 1s, and 2s.

Code:

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)

for i in range(0,size):
    for j in range(i+1, size):
        if arr[i]>=arr[j]:
            temp = arr[i]
            arr[i] = arr[j]
            arr[j] = temp

print("After segregate 0s, 1s and 2s in an Array, Array is:",arr)


Input/Output:
Enter the size of the array: 4
Enter the Element of the array:
1
0
2
1
After segregate 0s, 1s and 2s in an Array, Array is: [0, 1, 1, 2]

Post a Comment

0 Comments