您的位置:首页 > 其它

把数组中负数放在前面,0放中间,正数放后面,并保持相对顺序

2013-04-03 23:38 337 查看
void reSortArray(int Array[], int len)
{

int NextNegPos = 0;
for (int idx = 0; idx < len; idx++)
{
if (Array[idx] < 0)
{
int tmp = Array[idx];

for (int j = idx-1; j >= NextNegPos; j--)
{
Array[j+1] = Array[j];
}
Array[NextNegPos] = tmp;
NextNegPos++;
}
}

int NextZeroPos = NextNegPos;
for (int idx = NextZeroPos; idx < len; idx++)
{
if (Array[idx] == 0)
{
for (int j = idx-1; j >= NextZeroPos; j--)
{
Array[j+1] = Array[j];
}
Array[NextZeroPos] = 0;
NextZeroPos++;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐