Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions C++/ArrayList/sort-binary-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
sort binary array
Object oriented code
author : singhanandVEVO

*/
#include<bits/stdc++.h>
using namespace std;

class BinaryArray {

public:
int i,N;
vector<int> A;

void setN(int n) {
N = n;
}

void sortBinaryArray() {
if(N==1)
return;
int left = -1;
for(i=0;i<N;i++) {
if(A[i]==0) {
left++;
swap(A[left], A[i]);
}
}

}

void takeInput() {
A.clear();
int val;
for(i=0;i<N;i++) {
cin>>val;
A.push_back(val);

}
}

void printArray() {
for(i=0;i<N;i++) {
cout<<""<<A[i]<<" ";
}
cout<<endl;
}

void processTestCase() {
takeInput();
sortBinaryArray();
printArray();
}

};


int main(){

int T;
cin>>T;
BinaryArray B;
while(T--) {
int n;
cin>>n;
B.setN(n);
B.processTestCase();
}
return 0;
}