0

As far as I remember it was called(3,4 years ago) partition step and it was part of a quicksort but I am not sure.

I have to spllt element in the array against a condition for instance condition: x < 5

For array:

7,2,7,9,4,2,8

The result would be:

2,4,2,7,7,8,9 

The order of the elements does not matter. It supposed to be done within one for loop without embeded for loops inside.

I am looking for a pseudo or c++ code for how to swap elements against that kind of "pivot" condtiion.

The code I've already managed to create:

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <functional>
using namespace std;

template <typename T>

size_t partition(T arr[], size_t size, function<bool(T)> fun) {
    //here I would like to split it against the fun():boolean result
    return 4;
}

template <typename T>
void printTable(T arr[], size_t size) {
    for (int i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << endl;
}

template <typename T> bool less10(T a) {
    return a < 10;
}
int main(){
    cout << "BLABLA" << endl;
    int arri[] = { 1, 20, 3, 50, 6, 7 };
    size_t sizi = sizeof(arri) / sizeof(arri[0]); printTable(arri, sizi);
    size_t fi = partition(arri, sizi, (function<bool(int)>)less10<int>);
    printTable(arri, sizi);
    cout << "index: " << fi << endl; cout << endl;
    double arrd[] = { 1, 20, 3, 50, 6, 7 };
    size_t sizd = sizeof(arrd) / sizeof(arrd[0]); printTable(arrd, sizd); function<bool(double)> lambda =
        [](double x) -> bool {return x > 10; }; size_t fd = partition(arrd, sizd, lambda); printTable(arrd, sizd);
    cout << "index: " << fd << endl;
    system("PAUSE");
    return 0;
}
2
  • Is there some reason not to use std::partition? Commented Apr 20, 2014 at 17:26
  • @AlanStokes Yes, I do that for a student and I wanted to prepare that for next lesson and thus I am looking for a psuedo code. Commented Apr 20, 2014 at 17:28

1 Answer 1

2

You could code it like so

#include <algorithm>

int main()
{
    int ar[] = {7,2,7,9,4,2,8};

    std::partition(std::begin(ar), std::end(ar), [](int val) {
        return val < 5; });
}

If implementation of std::partition is what matters you could check a trivial implementation

template <class BidirectionalIterator, class UnaryPredicate>
BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    while (pred(*first)) {
      ++first;
      if (first==last) return first;
    }
    do {
      --last;
      if (first==last) return first;
    } while (!pred(*last));
    swap (*first,*last);
    ++first;
  }
  return first;
}
Sign up to request clarification or add additional context in comments.

2 Comments

the poster explicitly asked for an answer without std::partition in the comments to the question
@niklasfi That was specified at the comments at about the time I was posting the answer, so only saw it afterwards. I provided a trivial implementation of std::partition to complement the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.