On 22.08.2026 13:02, Jan Dubiec wrote: > See the comment below. I don't know why qsort behaves so strangely. It > could be a bug, or simply a consequence of its being an unstable sort. > Unfortunately, I don't have time to investigate the root cause. OK, I did some testing that shows that Microsoft's implementation of qsort() uses an unstable sorting algorithm, i.e. the order of equal elements may change. See the attached program. It compares insertion sort, which is a stable algorithm, with qsort(). The test fails on Windows and passes on Linux. The compiler is irrelevant — both Microsoft's compiler and GCC from MinGW produce the same result. It's worth noting that although the test passes on Linux, this is not guaranteed. According to AI, the qsort() implementation in glibc generally uses merge sort (stable), but may fall back to heapsort (unstable), depending on the size of the data. The glibc documentation seems to confirm this — it clearly states that "If two elements compare equal, their order after sorting is unpredictable": https://sourceware.org/glibc/manual/2.44/html_node/Array-Sort-Function.html So I think the best solution is to use a sorting algorithm known to be stable on every host, e.g. the insertion sort implementation from my previous message. What do you think? /J.D.