Quicksort Troubles

I am trying to make a function that uses the quicksort method of sorting. The thing I am having trouble with is returning two separate vectors while recurring them. Here is my quickSort function as of now:

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
vector <char> quickSort(vector <char> Letters)
{
    if (Letters.size() == 1)
    return Letters;

    int pivot = (rand() % Letters.size());

    vector <char> Less;
    vector <char> Greater;

    Less.reserve(Letters.size());
    Greater.reserve(Letters.size());

    for (int i = 0; i < Letters.size(); i++)
    {
        if (Letters[i] > Letters[pivot])
        {
            Greater.push_back(Letters[i]);
        }
        else
        {
            Less.push_back(Letters[i]);
        }
    }

    //PSEUDO
    return quickSort(Less) + quickSort(Greater);
}


Any nudge in the right direction would be greatly appreciated.
I need to recur both vectors though, so how would I go about doing that? Maybe more than a nudge :)
quicksort can be done in-place.
Instead of passing a vector, you could pass iterators
1
2
template<class iterator>
void quicksort(iterator begin, iterator end); // range [) 


If you still want to do it trough copy:
1
2
3
Less = quicksort(Less);
Greater = quicksort(Greater);
return /* Less + Greater */;
Last edited on
How would I edit my code to work properly? At least to return properly? I should be able to figure out any other problems though.
Last edited on
Topic archived. No new replies allowed.