Sort 0 1 2

 Sort 0 1 2

Send Feedback

You are given an integer array/list(ARR) of size N. It contains only 0s, 1s and 2s. Write a solution to sort this array/list in a 'single scan'.

'Single Scan' refers to iterating over the array/list just once or to put it in other words, you will be visiting each element in the array/list just once.

Note:
You need to change in the given array/list itself. Hence, no need to return or print anything. 
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.

First line of each test case or query contains an integer 'N' representing the size of the array/list.

Second line contains 'N' single space separated integers(all 0s, 1s and 2s) representing the elements in the array/list.
Output Format :
For each test case, print the sorted array/list elements in a row separated by a single space.

Output for every test case will be printed in a separate line.
Constraints :
1 <= t <= 10^2
0 <= N <= 10^5
Time Limit: 1 sec
Sample Input 1:
1
7
0 1 2 0 2 0 1
Sample Output 1:
0 0 0 1 1 2 2 
Sample Input 2:
2
5
2 2 0 1 1
7
0 1 2 0 1 2 0
Sample Output 2:
0 1 1 2 2 
0 0 0 1 1 2 2

// void sort012(int *arr, int n)
// {
// //Write your code here
// int count=0;
// for(int i=0;n>i;i++){
// if(arr[i]==0){
// count++;
// }
// }
// static int j=0;
// for( j=0;count>j;j++){
// arr[j]=0;
// }
// for(int i=0;n>i;i++){
// if(arr[i]==1)
// count++;
// }
// for(;count>j;j++){
// arr[j]=1;
// }
// for(int i=0;n>i;i++){
// if(arr[i]==2)
// count++;
// }
// for(;count>j;j++){
// arr[j]=2;
// }
// }

/*
int count =0;
for (int i = 0; i < size; i++) {
if (input[i] == 0)
count++;
}
for (int i = 0; i < count; i++){
input[i] = 0;
}
for (int i = count; i < size; i++) {
input[i] = 1;
}
*/

// void sort012(int *arr, int n)
// {
// int count=0;
// for(int i=0;n>i;i++){
// if(arr[i]==0)
// count++;
// }
// for(int i=0;n>i;i++){
// arr[i]=0;
// }
// for(int i=0;n>i;i++){
// if(arr[i]==1)
// count++;
// }
// for(int i=0;n>i;i++){
// if(arr[i]==1)
// count++;
// }
// for(;count>j;j++){
// arr[j]=1;
// }
// }




void sort012(int *a, int n)
{
int count_0=0;
int count_1=0;
int count_2=0;
for(int i=0;i<n;i++)
{
if(a[i]==0)
count_0++;
else if(a[i]==1)
count_1++;
else
count_2++;
}
for(int i=0;i<count_0;i++)
{
a[i]=0;
}
for(int i=count_0;i<(count_0+count_1);i++)
{
a[i]=1;
}
for(int i=(count_1+count_0);i<n;i++)
{
a[i]=2;
}
}

// void sort012(int *arr, int n){
// int count_0=0;
// int count_1=0;
// int count_2=0;
// for(int i=0;i<n;i++){
// if(arr[i]==0)
// count_0++;
// else if(arr[i]==1)
// count_1++;
// else
// count_2++;
// }
// for(int i=0;count_0<i;i++){
// arr[i]=0;
// }
// for(int i=count_0; i<(count_0 + count_1);i++){
// arr[i]=1;
// }
// for(int i= (count_0 + count_1);i<n;i++){
// arr[i]=2;
// }
// }


#include <iostream>
#include <algorithm>
using namespace std;

// void sort012(int *arr, int n)
// {
// //Write your code here
// int count=0;
// for(int i=0;n>i;i++){
// if(arr[i]==0){
// count++;
// }
// }
// static int j=0;
// for( j=0;count>j;j++){
// arr[j]=0;
// }
// for(int i=0;n>i;i++){
// if(arr[i]==1)
// count++;
// }
// for(;count>j;j++){
// arr[j]=1;
// }
// for(int i=0;n>i;i++){
// if(arr[i]==2)
// count++;
// }
// for(;count>j;j++){
// arr[j]=2;
// }
// }

/*
int count =0;
for (int i = 0; i < size; i++) {
if (input[i] == 0)
count++;
}
for (int i = 0; i < count; i++){
input[i] = 0;
}
for (int i = count; i < size; i++) {
input[i] = 1;
}
*/

// void sort012(int *arr, int n)
// {
// int count=0;
// for(int i=0;n>i;i++){
// if(arr[i]==0)
// count++;
// }
// for(int i=0;n>i;i++){
// arr[i]=0;
// }
// for(int i=0;n>i;i++){
// if(arr[i]==1)
// count++;
// }
// for(int i=0;n>i;i++){
// if(arr[i]==1)
// count++;
// }
// for(;count>j;j++){
// arr[j]=1;
// }
// }




void sort012(int *a, int n)
{
int count_0=0;
int count_1=0;
int count_2=0;
for(int i=0;i<n;i++)
{
if(a[i]==0)
count_0++;
else if(a[i]==1)
count_1++;
else
count_2++;
}
for(int i=0;i<count_0;i++)
{
a[i]=0;
}
for(int i=count_0;i<(count_0+count_1);i++)
{
a[i]=1;
}
for(int i=(count_1+count_0);i<n;i++)
{
a[i]=2;
}
}

// void sort012(int *arr, int n){
// int count_0=0;
// int count_1=0;
// int count_2=0;
// for(int i=0;i<n;i++){
// if(arr[i]==0)
// count_0++;
// else if(arr[i]==1)
// count_1++;
// else
// count_2++;
// }
// for(int i=0;count_0<i;i++){
// arr[i]=0;
// }
// for(int i=count_0; i<(count_0 + count_1);i++){
// arr[i]=1;
// }
// for(int i= (count_0 + count_1);i<n;i++){
// arr[i]=2;
// }
// }

int main()
{
int t;
cin >> t;
while (t--)
{
int size;

cin >> size;
int *arr = new int[size];

for (int i = 0; i < size; i++)
{
cin >> arr[i];
}

sort012(arr, size);

for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}

delete[] arr;
cout << endl;
}

return 0;
}

Comments

Popular posts from this blog

Code : All connected components

Coding Ninjas