劍指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;
}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章