调整数组使奇数位于偶数前面
题目:
思路:
代码:
#include<stdio.h>
#include<string.h>
void func(int* arr, int len) {int left = 0;int right = len - 1;while (left < right) {while (left < right && arr[left] % 2 != 0) {left++;}while (left < right && arr[right] % 2 == 0) {right--;}//如果left和right相等则自身和自身交换故if(left<right)可加可不加arr[left] = arr[left] ^ arr[right];arr[right] = arr[left] ^ arr[right];arr[left] = arr[left] ^ arr[right];}
}
int main() {int arr[] = { 1,2,3,4,5,6 };int len = sizeof(arr) / sizeof(arr[0]);int i = 0;func(arr, len);for (i = 0; i < len; i++) {printf("%d ", arr[i]);}return 0;
}