How would you sort an array that consists of only zeros and ones in only one pass?
Make two points p1 and p2 pointing to the first element,
foo(int*a, int len)
{
int* p1=a;
int* p2=a[len-1];
while(p1!=p2)
{
if(*p1==0&&*p2==1)
{p1++;p2--;}
eles if(*p1==0&&*p2==0)
{p1++;}
else if(*p1==1&&*p2==0)
{*p1=0;*p2=1;p1++;p2--;}
else//1 1
{p2--;}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment