【c语言】调整数组使奇数所有都位于偶数前面

//  调整数组使奇数全部都位于偶数前面
//  输入一个整数数组,实现一个函数,来调整该数组中数字的顺序使得数组中全部的奇数位于数组的前半部分,
//  全部偶数位于数组的后半部分。

#include <stdio.h> #include <assert.h> void johh(int *p, int len) { int *q = p + len - 1; int temp; assert(p != NULL); while (p < q) { while ((*p & 1) == 1) { p++; } while ((*q & 1) == 0) { q--; } if (p < q) { temp = *p; *p = *q; *q = temp; } } return; } int main() { int arr[] = {1,2,3,4,5,6,7,8,9}; int len = sizeof(arr) / sizeof(arr[0]); int i = 0; johh(arr, len); for (; i < len; ++i) { printf("%d ", arr[i]); } printf(" "); return 0; }






原文地址:https://www.cnblogs.com/blfshiye/p/5159356.html