在O(N)时间中使用布尔谓词函数对向量排序

时间:2019-03-21 22:59:31

标签: c++ sorting stl

作为一个示例,假设我要对向量{1、2、3、4、5}进行排序,将偶数放在左边,奇数放在右边。我可以设计一种算法,以O(N)的时间执行此操作(如下所示)。我的问题是,是否存在用于此类操作的现有STL算法?

我的解决方案(不是很通用或很漂亮)

#include <iostream>
#include <vector>

/**
 Sort a vector of integers according to a boolean predicate function

 Reorders the elements of x such that elements satisfying some condition
 (i.e. f(x) = true) are arranged to the left and elements not satisfying the
 condition (f(x) = false) are arranged to the right
 (Note that this sort method is unstable)

 @param x vector of integers
 */
void sort_binary(std::vector<int>& x, bool (*func)(int)){
    // Strategy:
    // Simultaneously iterate over x from the left and right ends towards
    // the middle. When one finds {..., false, ..., ..., true, ....},
    // swap those elements

    std::vector<int>::iterator it1 = x.begin();
    std::vector<int>::iterator it2 = x.end();
    int temp;

    while(it1 != it2){
        while(func(*it1) && it1 < it2){
            ++it1;
        }
        while(!func(*it2) && it1 < it2){
            --it2;
        }
        if(it1 != it2){
            // Swap elements
            temp = *it1;
            *it1 = *it2;
            *it2 = temp;
        }
    }
}

int main() {
    // Sort a vector of ints so that even numbers are on the
    // left and odd numbers are on the right
    std::vector<int> foo {1, 2, 3, 4, 5};
    sort_binary(foo, [](int x) { return x % 2 == 0; } );
    for(auto &x : foo) std::cout << x << " ";
}

1 个答案:

答案 0 :(得分:4)

您可以使用std::partition()

  

重新排列范围[first,last)中的元素,以使谓词p返回true的所有元素都在谓词p返回false的元素之前。元素的相对顺序不保留。

复杂性:

  

该谓词恰好有N个应用。如果ForwardIt满足LegacyBidirectionalIterator的要求,最多N / 2个交换,否则最多N个交换。

std::partition( foo.begin(), foo.end(), [](int x) { return x % 2 == 0; } );

live example

如果要保留元素的顺序,可以使用std::stable_partition() PS

相关问题