给定一个数组,有正数,负数和零,排列给定的数组,,使负数在左边,0在中间,正数在右边。
Given an array with positive, negative and zeros, arrange the given array such that negatives are on left, zeros in the middle and positives on the right.
这个问题可以用荷兰国旗问题来解决。
用负数代替0,0代替1,正数代替2
This problem can be solved by modified Dutch National Flag problem.Instead of ‘0’ we have -ve numbersInstead of ‘1’ we have 0’s(zero).and for 2 we have +ve Numbers.
void swap(int * a,int * b){int c;c = *a;*a = *b;*b = c;}void sortNumbers(int *arr,int len){int low = 0,mid = 0,high = len – 1;while (mid <= high){if (arr[mid] < 0 ){swap(&arr[low++],&arr[mid++]);}elseif (arr[mid] == 0){mid++;}elseswap(arr[mid],arr[high–]);}}
重新开始吧!下次我会吸取教训,不让自己犯同样的错误的;