【面试题十四】调整数组顺序使奇数位于偶数前面

来源:互联网 发布:免费淘宝店标在线制作 编辑:程序博客网 时间:2024/06/14 09:19

调整数组顺序使奇数位于偶数前面

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

1.第一个指针初始化为指向数组的第一个数字,他向后移动

2.第二个指针初始化为指向数组的最后一个数字,他向前面移动,

3.在两个指针相遇之前,第一个指针总是位于第二个指针的前门,

4.如果第一个指针指向的数字是偶数,并且第二个指针指向的数字是奇数,我们就交换两个数字,否者如果是一个满足,或者都不满足就按照规则移动指针;


Reorder.cpp:

#include <iostream>#include <cstdio>using namespace std;void Reorder(int *pData, unsigned int length, bool (*func)(int));bool isEven(int n);/*方法一*/void ReorderOddEven_1(int *pData, unsigned int length){    if(pData == NULL || length == 0)        return;    int *pBegin = pData;    int *pEnd = pData + length - 1;    while(pBegin < pEnd)    {        // 向后移动pBegin,直到它指向偶数        while(pBegin < pEnd && (*pBegin & 0x1) != 0)            pBegin ++;        // 向前移动pEnd,直到它指向奇数        while(pBegin < pEnd && (*pEnd & 0x1) == 0)            pEnd --;        if(pBegin < pEnd)        {            int temp = *pBegin;            *pBegin = *pEnd;            *pEnd = temp;        }    }}/*方法二*/void ReorderOddEven_2(int *pData, unsigned int length){    Reorder(pData, length, isEven);}void Reorder(int *pData, unsigned int length, bool (*func)(int)){    if(pData == NULL || length == 0)        return;    int *pBegin = pData;    int *pEnd = pData + length - 1;    while(pBegin < pEnd)     {        // 向后移动pBegin        while(pBegin < pEnd && !func(*pBegin))            pBegin ++;        // 向前移动pEnd        while(pBegin < pEnd && func(*pEnd))            pEnd --;        if(pBegin < pEnd)        {            int temp = *pBegin;            *pBegin = *pEnd;            *pEnd = temp;        }    }}bool isEven(int n){    return (n & 1) == 0;}/*测试代码*/void PrintArray(int numbers[], int length){    if(length < 0)        return;    for(int i = 0; i < length; ++i)        printf("%d\t", numbers[i]);    printf("\n");}void Test(char* testName, int numbers[], int length){    if(testName != NULL)        printf("%s begins:\n", testName);    int* copy = new int[length];    for(int i = 0; i < length; ++i)    {        copy[i] = numbers[i];    }    printf("Test for solution 1:\n");    PrintArray(numbers, length);    ReorderOddEven_1(numbers, length);    PrintArray(numbers, length);    printf("Test for solution 2:\n");    PrintArray(copy, length);    ReorderOddEven_2(copy, length);    PrintArray(copy, length);    delete[] copy;}void Test1(){    int numbers[] = {1, 2, 3, 4, 5, 6, 7};    Test("Test1", numbers, sizeof(numbers)/sizeof(int));}void Test2(){    int numbers[] = {2, 4, 6, 1, 3, 5, 7};    Test("Test2", numbers, sizeof(numbers)/sizeof(int));}void Test3(){    int numbers[] = {1, 3, 5, 7, 2, 4, 6};    Test("Test3", numbers, sizeof(numbers)/sizeof(int));}void Test4(){    int numbers[] = {1};    Test("Test4", numbers, sizeof(numbers)/sizeof(int));}void Test5(){    int numbers[] = {2};    Test("Test5", numbers, sizeof(numbers)/sizeof(int));}void Test6(){    Test("Test6", NULL, 0);}int main(){    Test1();    Test2();    Test3();    Test4();    Test5();    Test6();    return 0;}


Makefile:

.PHONY:cleanCPP=g++CFLAGS=-Wall -gBIN=testOBJS=Reorder.oLIBS=$(BIN):$(OBJS)$(CPP) $(CFLAGS) $^ -o $@ $(LIBS)%.o:%.cpp$(CPP) $(CFLAGS) -c $< -o $@clean:rm -f *.o $(BIN)


运行结果:


Test1 begins:Test for solution 1:1       2       3       4       5       6       71       7       3       5       4       6       2Test for solution 2:1       2       3       4       5       6       71       7       3       5       4       6       2Test2 begins:Test for solution 1:2       4       6       1       3       5       77       5       3       1       6       4       2Test for solution 2:2       4       6       1       3       5       77       5       3       1       6       4       2Test3 begins:Test for solution 1:1       3       5       7       2       4       61       3       5       7       2       4       6Test for solution 2:1       3       5       7       2       4       61       3       5       7       2       4       6Test4 begins:Test for solution 1:11Test for solution 2:11Test5 begins:Test for solution 1:22Test for solution 2:22Test6 begins:Test for solution 1:Test for solution 2:


原创粉丝点击