标签:
#include<stdio.h> void change_array(int *p, int len) { if (p == NULL || len == 0) return; int *begin = p; int *end = p + len - 1; while (begin < end) { while ((begin < end) && ((*begin%2)!=0)) begin++; while ((begin < end) && ((*end %2)== 0)) end--; if (begin < end) { int temp = *begin; *begin = *end; *end = temp; } } } void main() { int a[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; int i = 0; change_array(a, sizeof(a) / sizeof(a[0])); while (i < sizeof(a) / sizeof(a[0])) { printf("%d ", a[i]); i++; } return; } 这种做法时间复杂度为O(n),如果从前往后顺序遍历数组每遇到偶数就将其移到数组末尾时间复杂度为O(n2)
标签:
原文地址:http://blog.csdn.net/yangshuangtao/article/details/46506705