一个数组中有三种数,负数,零和正数,现在要求只对数组扫描一遍,即完成将数组分为三部分,负数、零和正数。尝试写了一下代码。
#include <stdio.h>
#define N 9
void swap(int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
return ;
}
int main()
{
int array[N]={-1,-3,0,-2,3,2,-2,0,1};
int *p_neg,*p,*p_pos;
for (p_neg=&array[0],p=&array[0],p_pos=&array[N-1]; p <= p_pos; p++)
{
if (*p < 0)
{
swap(p,p_neg);
p_neg++;
}
if (*p > 0)
{
swap(p,p_pos);
p_pos--;
p--;
}
}
;
}
本文出自 “天才鸟蛋” 博客,请务必保留此出处http://curley.blog.51cto.com/1627940/1297308