I’m using the following code to sort elements in a std.vector
struct element {
int order;
int someValue;
};
int comp( element a, element b ) {
if (a.order < b.order)
return true;
return (rand() % 2) == 0;
}
vector.quickSort(comp);
is this code correct if I’m trying to add randomness on the elements that have the same order value
You should really write a test with some data to check that.
If you would write a test you would see something wrong and know why, if we look at
you can see that the
a.order > b.ordercase is missing which is why it will incorrectly work for that case, we can fix this by doing this instead which focuses on the exceptional case instead and leaves the other two cases to their default behavior (checking the order):