Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Sort_Array_zero_one_two.cpp #3117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
26 changes: 12 additions & 14 deletions Sort_Array_zero_one_two/Sort_Array_zero_one_two.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ using namespace std;

void sort_zero_one_two(int a[],int n)
{
int zero = 0; //counter to store number of 0's
int one = 0; //counter to store number of 1's
for(int i = 0 ;i < n; i++)
{
if(a[i] == 0)
zero++;
else if(a[i] == 1)
one++;
int count = 0;
for(int i=0;i<n;i++){
if(a[i]==0){
swap(a[i],a[count]);
count++;
}
}
for(int i=count;i<n;i++){
if(a[i]==1){
swap(a[i],a[count]);
count++;
}
}
for(int i = 0;i < zero;i++)
a[i] = 0;
for(int i = zero;i< zero+one;i++)
a[i] = 1;
for(int i = one+zero;i < n;i++) //filling rest of the array with 2
a[i] = 2;
}

int main()
Expand Down