实现一个排序,让数组中,负数最前面,0中间,正数最后,本次不要求保持有序,双指针思想
static void threesort(int[] x) {
int p = 0;
int left = 0;
int right = x.length - 1;
while (p <= right) {
if (x[p] < 0) {
int t = x[left];
x[left] = x[p];
x[p] = t;
left++;
p++;
} else if (x[p] > 0) {
int t = x[right];
x[right] = x[p];
x[p] = t;
right--;
} else {
p++;
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)