算法 - 把数组元素按奇偶性分开并排序(C++)

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程

/*
 * Created by Chimomo
 */

#include <iostream>
#include "../HeapSort.cpp"
#include "OddEvenSplitter.cpp"

using namespace std;

int main() {
    int a[] = {34, 6, 78, 9, 222, 35, 0, -98, 23, 1};
    cout << "Original array: ";
    for (int i : a) {
        cout << i << " ";
    }
    cout << endl;

    OddEvenSplitter::Split(a, 10);
    cout << "Odd-even splitted array: ";
    for (int i : a) {
        cout << i << " ";
    }
    cout << endl;

    int point = OddEvenSplitter::GetSplittingPoint(a, 10);
    if (point == 0 || point == 9) {
        HeapSort<int>::Sort(a, 10);
    } else {
        HeapSort<int>::Sort(a, point);
        HeapSort<int>::Sort(a + point, 10 - point);
    }
    cout << "Sorted odd-even splitted array: ";
    for (int i : a) {
        cout << i << " ";
    }
    cout << endl;
}

// Output:
/*
Original array: 34 6 78 9 222 35 0 -98 23 1
Odd-even splitted array: 1 23 35 9 222 78 0 -98 6 34
Sorted odd-even splitted array: 1 9 23 35 -98 0 6 34 78 222

*/
/*
 * HeapSort.cpp - by Chimomo
 */

template<class T>
class HeapSort {
public:
    /**
     * Heap sort.
     */
    static void Sort(T a[], int length) {
        BuildMaxHeap(a, length);
        for (int i = length - 1; i > 0; i--) {
            Swap(a[0], a[i]);
            MaxHeaping(a, 0, i);
        }
    }

private:
    /**
     * Build max heap.
     * @param a The array.
     * @param length The length of array.
     */
    static void BuildMaxHeap(T a[], int length) {
        for (int i = length / 2 - 1; i >= 0; i--) {
            MaxHeaping(a, i, length);
        }
    }

    /**
     * Adjust to max heap.
     * @param a The array.
     * @param i The element to be adjusted.
     * @param heapSize The heap size.
     */
    static void MaxHeaping(T a[], int i, int heapSize) {
        int left = 2 * i + 1;
        int right = 2 * (i + 1);
        int large = i;
        if (left < heapSize && a[left] > a[large]) {
            large = left;
        }
        if (right < heapSize && a[right] > a[large]) {
            large = right;
        }
        if (i != large) {
            Swap(a[i], a[large]);
            MaxHeaping(a, large, heapSize);
        }
    }

    /**
     * Swap.
     * @param a The a.
     * @param b The b.
     */
    static void Swap(T &a, T &b) {
        T t = a;
        a = b;
        b = t;
    }
};
/*
 * OddEvenSplitter.cpp - by Chimomo
 */

class OddEvenSplitter {
public:
    /**
     * Split odd even numbers in an array, make odd numbers be at the front half and even numbers be at the rear half.
     * @param a The array.
     * @param length The array length.
     */
    static void Split(int a[], int length) {
        int i = 0;
        int j = length - 1;
        while (i < j) {
            if (IsOdd(a[i])) {
                i++;
            }
            if (IsEven(a[j])) {
                j--;
            }
            if (IsEven(a[i]) && IsOdd(a[j])) {
                Swap(a[i], a[j]);
                i++;
                j++;
            }
        }
    }

    /**
     * Get the splitting point.
     * @param a The array.
     * @param length The array length.
     * @return The element index.
     */
    static int GetSplittingPoint(int a[], int length) {
        int i;
        for (i = 0; i < length; i++) {
            if (IsEven(a[i])) {
                return i;
            }
        }
    }

private:
    static bool IsOdd(int i) {
        if (i % 2 == 1) {
            return true;
        } else {
            return false;
        }
    }

    static bool IsEven(int i) {
        return !IsOdd(i);
    }

    static void Swap(int &a, int &b) {
        int t = a;
        a = b;
        b = t;
    }
};

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值