Consider an array of only 1s and 0s. Write an algorithm that will separate all 1s and all 0s.
Try to do it in one parse of array.
Here is the running code…
void sort()
{
int a[]={1,0,0,0,1,1,0,1,0,1,0,0,1,0};
int j=13;
int i=0;
int temp;
while(j>i)
{
if(a[i]==1)
i++;
if(a[j]==0)
j--;
if(a[i]==0)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0;i<14;i++)
Console.Write(a[i]+", ");
}
Output: 1,1,1,1,1,1,0,0,0,0,0,0,0
Happy Learning...