将一个数组中的数右边为复数,左边为正数,中间为0(不考虑大小)
#include<iostream>
using namespace std;
void sort(int* a, int len)//大于0向右,小于0向左边,等于0在中间
{
int mod = 0;
int left = 0;
int right = len-1;
while (mod <= right)
{
if (a[mod] < 0)//小于0向左边
{
int tmp;
tmp = a[left];
a[left] = a[mod];
a[mod] = tmp;//用mod一个一个去移动判断
left++;
mod++;
}
else if (a[mod] > 0)//大于0向右
{
int tmp;
tmp = a[right];
a[right] = a[mod];
a[mod] = tmp;
right--;
}
else//都等于0
{
mod++;
}
}
}
int main()
{
int arr[11] = { -2,-6,-3,1,0,2,4,0,8,-1,9 };
sort(arr, 11);
for (int i = 0; i < 11; i++)
{
cout << arr[i] << " ";
}
}