Reputation: 129
It's the assignment from student work, the task was to write sort using std::nth_element
and iterators. I've done this:
template <typename It> void quick_sort(It beg, It end)
{
if (beg == end || std::distance(beg, end) == 1) return;
auto dist = std::distance(beg, end) / 2;
std::nth_element(beg, std::next(beg, dist), end);
}
And it worked only on some inputs. Then I asked a mate about his implementation. And apart that he used one explicit variable to store distance, it was just the same. To be sure I copied his code but nothing changed. I've read that "Elements are hypothetically sorted with respect to operator<(until C++20)". But for all the people using VS it works and as I'm the only using Linux, so have nobody else to check. To do as much as I can, I tried clang18, but it resulted the same issue. Points for that task now are gone, as I was said at the beginning of the semester to use Windows as all the others, but I just want to know why the things are like this. Main is:
int main()
{
std::vector<int> v{2, 5, 6, 1, 7, 0};
std::vector<int> v_sorted{0, 1, 2, 5, 6, 7};
quick_sort(v.begin(), v.end());
assert(v == v_sorted);
As I see, the border pairs for some input are not sorted.
To add to changing compilers I've also tried to change the second argument for nth_element to random iterator in range argument vector, nothing changed.
Upvotes: 0
Views: 50