剑指offer-调整数组顺序使得数组中奇数位于偶数前面

/*******************************************************************
Copyright(c) 2016, Tyrone Li
All rights reserved.
*******************************************************************/
// 作者:TyroneLi
//

/*
Q:
    输入一个整数数组,实现一个函数来调整该数组中数字的顺序,
    使得所有的奇数位于数组的前半部分,所有偶数位于数组的后半部分。
S:
    实现一遍遍历数组,也就是在O(1)时间复杂度内实现。
    使用两个指针,一个指向数组的最开始,另一个指向数组的最后面,
    然后第一个数组向后移动,另一个指针向前移动,一旦第一个指针指向
    的数据是偶数,第二个指针指向的数据是奇数,那么交换两者的数据,
    直到第二个指针位于第一个指针的前面,结束算法。
*/

#include "../utils/List.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

void reOrderOddEven(int arr[], int length)
{
    if(arr == nullptr || length <= 0)
        return;
    
    int*pAhead = arr;
    int*pBehind = arr + length - 1;
    while(pAhead < pBehind)
    {
        if(pBehind < pAhead)
            return;
        while(pAhead < pBehind && (*(pAhead) & 0x01) != 0)
            ++pAhead;
        
        while(pAhead < pBehind && (*(pBehind) & 0x01) == 0)
            --pBehind;
        
        if(pAhead < pBehind)
        {
            int tmp = *pAhead;
            *pAhead = *pBehind;
            *pBehind = tmp;
        }
    }
}

/* 扩展版--使得满足某种条件的数组重新排列问题抽象出来建立一种模式 */
void reOrderOddEven_better(int arr[], int length, bool(*func)(int))
{
    if(arr == nullptr || length <= 0)
        return;
    
    int*pAhead = arr;
    int*pBehind = arr + length - 1;
    while(pAhead < pBehind)
    {
        if(pBehind < pAhead)
            return;
        while(pAhead < pBehind && ((func)(*pAhead)))
            ++pAhead;
        
        while(pAhead < pBehind && !((func(*pBehind))))
            --pBehind;
        
        if(pAhead < pBehind)
        {
            int tmp = *pAhead;
            *pAhead = *pBehind;
            *pBehind = tmp;
        }
    }
}

bool is_Odd(int value)
{
    return (value & 0x01) == 1 ?true:false;
}

bool is_negative(int value)
{
    return (value < 0) ?true:false;
}

bool is_module3(int value)
{
    return (value % 3) == 0 ? true:false;
}

void test_1()
{
    std::cout << "test 1" << std::endl;
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven(arr, 10);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_2()
{
    std::cout << "test 2" << std::endl;
    int arr[] = {1,3,5,7,9,2,4,6,8,10};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven(arr, 10);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_3()
{
    std::cout << "test 3" << std::endl;
    int arr[] = {2,4,6,8,10,1,3,5,7,9};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven(arr, 10);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_4()
{
    std::cout << "test 4" << std::endl;
    int arr[] = {1};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven(arr, 1);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_5()
{
    std::cout << "test 5" << std::endl;
    int arr[] = {3};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven(arr, 1);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_6()
{
    std::cout << "test 6" << std::endl;
    int arr[] = {};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven(arr, 0);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_7()
{
    std::cout << "test 7" << std::endl;
    int arr[] = {1,2};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven(arr, -1);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_8()
{
    std::cout << "test 8" << std::endl;
    int arr[] = {-1,-2,-3,-4,0,6,7,8,9,10};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven_better(arr, 10, is_negative);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_9()
{
    std::cout << "test 9" << std::endl;
    int arr[] = {6,7,8,9,10, 0, -1,-2,-3,-4,0};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven_better(arr, 10, is_negative);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_10()
{
    std::cout << "test 10" << std::endl;
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven_better(arr, 10, is_module3);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_11()
{
    std::cout << "test 11" << std::endl;
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
    reOrderOddEven_better(arr, 10, is_Odd);
    for(auto e : arr)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_reOrderOddEven()
{
    test_1();
    test_2();
    test_3();
    test_4();
    test_5();
    test_6();
    test_7();
}

void test_reOrderOddEven_better()
{
    test_8();
    test_9();
    test_10();
    test_11();
}

int main(int argc, char**argv)
{

    test_reOrderOddEven();
    test_reOrderOddEven_better();

    return 0;
}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章