如何简化彼此相似的两个功能

时间:2015-05-11 12:50:59

标签: c++ algorithm sorting quicksort

我是一名学习计算机工程的学生。 今天我用C ++学习快速排序。 它是如此棒的算法,我认识到快速排序需求 两个函数用于升序和相反的。 以下是我的代码!

#include <iostream>
#define ASCENDING 0 
#define DESCENDING 1
#define MAX_SIZE 50001

using namespace std;


int numberCnt;
int sortManner;
int list[MAX_SIZE];

void GetInput();
void QuickSort(int* list, int left, int right, int(*partition)(int*, int, int));
int PartitionAscending(int* list, int left, int right);
int PartitionDescending(int* list, int left, int right);
void Swap(int &a, int &b);

int main(){
    GetInput();
    QuickSort(list, 0, numberCnt - 1, sortManner == ASCENDING ? PartitionAscending : PartitionDescending);
    for (int i = 0; i < numberCnt; i++){
        cout << list[i] << endl;
    }

    return 0;
}

void QuickSort(int* list, int left, int right, int (*partition)(int*,int,int)){
    if (left < right){
        int pivot = partition(list, left, right);
        QuickSort(list, left, pivot - 1, partition);
        QuickSort(list, pivot + 1, right, partition);
    }
}
int PartitionAscending(int* list, int left, int right){
    int pivotVal = list[left];
    int pivotIdx = left;
    int low = left;
    int high = right + 1;

    do{
        do{
            low++;
        } while (list[low] < pivotVal);
        do{
            high--;
        } while (list[high] > pivotVal);
        if (low < high)
            Swap(list[low], list[high]);
    } while (low < high);

    Swap(list[pivotIdx], list[high]);

    return high;
}

int PartitionDescending(int* list, int left, int right){
    int pivotVal = list[left];
    int pivotIdx = left;
    int low = left;
    int high = right + 1;

    do{
        do{
            low++;
        } while (list[low] > pivotVal);
        do{
            high--;
        } while (list[high] < pivotVal);
        if (low < high)
            Swap(list[low], list[high]);
    } while (low < high);

    Swap(list[pivotIdx], list[high]);

    return high;
}

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

void GetInput(){
    cin >> numberCnt >> sortManner;
    for (int i = 0; i < numberCnt; i++)
        cin >> list[i];
}

你知道这些功能彼此非常相似! 这对我来说似乎很浪费!

如何简化功能?

如果你不懂我的游泳池英语 Plz,请不要犹豫,让我知道:)。

2 个答案:

答案 0 :(得分:2)

您的分区可以使用比较函数,例如:

template <typename Comp>
int Partition(int* list, int left, int right, Comp comp){
    int pivotVal = list[left];
    int pivotIdx = left;
    int low = left;
    int high = right + 1;

    do{
        do{
            low++;
        } while (comp(list[low], pivotVal));
        do{
            high--;
        } while (!comp(list[high], pivotVal));
        if (low < high)
            Swap(list[low], list[high]);
    } while (low < high);

    Swap(list[pivotIdx], list[high]);

    return high;
}

int PartitionAscending(int* list, int left, int right){
    return Partition(list, left, right, [](int l, int r){ return l < r; });
    // or return Partition(list, left, right, std::less<int>());
}

int PartitionDescending(int* list, int left, int right){
    return Partition(list, left, right, [](int l, int r){ return l > r; });
    // or return Partition(list, left, right, std::greater<int>());
}

答案 1 :(得分:0)

在函数中添加另一个参数,指定是否需要升序或降序调用,布尔值ascending可以这样做,并计算循环条件如下:

do{
   low++;
} while ( ascending ? (list[low] < pivotVal) : (list[low] > pivotVal));