# STL? sort

• 12-04-2005
suligab
STL? sort
hi there,
i have this problem:

i need to sort an array of objects so that an overall quality of the array to be maximum.
-the quality of the array depends on the order the points are in the array.
- the arrays i am supposed to sort are small (3-7 members)

for example: a={A B C D E} is the array and f(a) is the quality function.
Code:

`  f(a)=f(A)*f(A,B)*f(A,B,C)*f(B,C,D)*f(C,D,E)*f(E)`
is there a way i can use the STL sorting algorithms to do the sorting? or at least some of it?

I will appreciate even a hint about the right direction to be followed

Right now, i am generating all possible combinations and checking them all, so it "must" be a better way to do it than this. reducing the amount of computation to be done by chosing the A so that f(A) is best does not work, as there are cases with better f(a) which have a worse-than-optimal f(A).
thank you
• 12-04-2005
7stud
The STL sort algorithms can be used with a custom sort function, but the function needs to accept two arguments, i.e. the two elements to be compared, and then return true or false. So, you would have to develop your own function to determine which element would produce the overall lower f(a).
• 12-04-2005
suligab
but how can i overcome the fact that i can compute f(a) only after i "finished" sorting? maybe there is something "more powerfull" in STL that i do not know about?
• 12-04-2005
Daved
You can use the STL's next_permutation function to help you go through all the permutations, but I cannot think of a way to avoid checking all permutations through the STL.
• 12-06-2005
suligab
thank you daved. i read something about this function and it seems it might help a lot if i manage to get a good and fast comp object function.