placing all the 0s in even position and 1s in odd position in array of 0 and 1 s

int place01(int a[], int len)
{
int i = 0;
int j = 1;

while(i < len && j < len)
{
if(a[i] && !a[j])
{
a[i] = 0;
a[j] = 1;
i += 2;
j += 2;
}

if(!a[i])
i += 2;

if(a[j])
j += 2;
}

int t = i;
while(i < len)
{
if(!a[i])
{
a[t] = 0;
a[i] = 1;
t += 2;
}
i += 2;
}

t = j;
while(j < len)
{
if(a[j])
{
a[t] = 1;
a[j] = 0;
t += 2;
}
j += 2;
}
}
Advertisements

2 thoughts on “placing all the 0s in even position and 1s in odd position in array of 0 and 1 s

  1. excellent blog, but please please post atleast a few lines of explanation or pseudocode too. it would help every1 greatly. it is very difficult to have to read through only lines of code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s