Algorithms with comparator

How can I make Coctail Sort with comporator?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void CocktailSortTemplate(std::vector<int>& b, int n) {
    bool swapped = true;
    int start = 0;
    int end = n - 1;

    while (swapped){
        swapped = false;
        for (int i = start; i < end; ++i){
            if (b[i] > b[i + 1]) {
                std::swap(b[i], b[i + 1]);
                swapped = true;
            }
        }
        if (!swapped) break;
        swapped = false;
        --end;
        for (int i = end - 1; i >= start; --i){
            if (b[i] > b[i + 1]) {
                std::swap(b[i], b[i + 1]);
                swapped = true;
            }
        }
        ++start;
    }
}


I can make only:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
template <class RandomValue>
struct myclass {
    bool operator() (RandomValue i, RandomValue j) {
      return (i < j); }
} ;
template <class RandomIterator, class Compare, class T>
void CocktailSortComp(T& c, RandomIterator begin, RandomIterator end, Compare comp) {
    bool swapped = true;


    while (swapped){
        swapped = false;
        for (std::vector<int>::iterator it = c.begin(); it != c.end(); it++){
            if (comp (c[i+1] < c[i])) {
                std::swap(c[i], c[i + 1]);
                swapped = true;
            }
        }
        if (!swapped) break;
        swapped = false;
        --end;
        for ( std::vector<int>::iterator it = c.end() - 1; it != c.begin(); it--){
            if (comp (c[i+1] < c[i])) {
                std::swap(c[i], c[i + 1]);
                swapped = true;
            }
        }
        ++begin;
    }
}
if( comp(c[i+1], c[i]) ) // c[i+1] < c[i]
Last edited on
if( comp(c[i+1], c[i]) ) // c[i+1] < c[i]

thank
Topic archived. No new replies allowed.