Trying to perform a swap to sort a vector

Hello,

I'm having issues with a swap function to sort a vector from highest value to lowest or vice versa.
Below is the code and with the current swap function it isn't doing anything when sorting and I don't know why.

The swap calls are in the quickSort function.

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
31
32
33
34
35
36
37
38
  template <class T>
int partition(vector<T>& set, int start, int end, bool (*compare)(const T&, 
const T&))
{


int pivotIndex;
int mid;
T pivotValue;

mid = (start + end) / 2;

swap (set[start], set[mid]);

pivotIndex = start;
pivotValue = set[start];

for (int scan = start + 1; scan <= end; scan++)
  {
    if (compare(set[scan], pivotValue))
  {
    pivotIndex++;
swap (set[pivotIndex], set[scan]);
  }
  }

swap(set[start], set[pivotIndex]);

return pivotIndex;
}

template<class T>
void swap(vector<T>& a, vector<T>& b)
{
vector<T> temp = a;
a = b;
b = temp;
}
Hello ritzbitz,

Welcome to the forum. and thank you for using code tags.

I am having problems setting up this code to compile and test. I do not know what you did in main before calling the "partition" function.

Looking at your code I am not sure right now if it will even work. Sorting a vector is much like sorting an array. Right now I am not sure if you are going about the sort the right way.

The "swap" function I do not believe that this is correct. You are defining "temp" as a whole vector when an int will do. Swapping the values of two elements is all you need to do.

The function name "partition" does not describe what the function does very well. I would stay away from a name like "sort" or "Sort" and maybe call it "SortVector" to describe what the function does.

If you do not mind posting the whole code so I can see what you did and better be able to test the code.

Andy
Topic archived. No new replies allowed.