std::stable_sort vs std::sort

Revision en16, by willy108, 2021-03-09 02:33:46

This is my first blog post, so it probably will end up being scuffed.

==============================

The problem

So I was working on CF 472D, Design Tutorial: Inverse the Problem and it requires an MST (sorry for spoiling) and I chose to use Kruskals MST to do this (just note that I could've avoided this issue by using any other MST alg, but that's not the point here). So I just coded vanilla Kruskals where I std::sort'ed the edges and used a DSU with path compression and union by size. And I submitted the code and lo and behold, it was TLE on tc 39! I entered into a state of depression and decided to change my std::sort to stable sort and wow, it ac'ed (with 1934ms on a 2 second TL)! Well I was talking some people on Discord later and I this up and one of them told me that the only reason std::sort was slow was since I had used c++11 (which I did) instead of c++17 where std::sort was upgraded. So I submitted it again with std::sort and c++17 and yay it passed (with 1466ms)! and ~470ms faster than my last submission! But to satisfy my curiosity I submitted it again with c++17 and std::stable_sort and woah, it passed with 1154ms, some 310ms faster than the last one.

Please do not judge my template/coding style as that is not the point here, but if you look at my code, the only differences between the submissions are the submission settings and a different sort on line 65 (or the second line of the kruskals function).

the submissions from each correlate to:

  • 1154ms, c++17 with stable_sort

  • 1466ms, c++17 with sort

  • 1934ms, c++11 with stable_sort

  • TLE tc 39, c++11 with sort

also note I did not benchmark memory since that was not something that bothered me in all of this

===============================

The resolution?

So here is one (of probably very few) situations where std::stable_sort outperformed std::sort. The problem that I have right now is that I do not want to miss a submission in a future contest over which sort I chose, since there are definitely problems out here where this problem cannot be avoided by something just as simple as using Prim instead of Kruskal.

If anyone has a good suggestion of which to use (std::stable_sort or std::sort) or a simple impl of a sorting alg that I can write up a little more time for similar results, please link it. I will probably just end up using stable_sort from now on since that was what made a difference here unless there is a really convincing argument or implementation that'll make me switch. No upvoting is needed (unless you want to :D), I just need the help. Thanks for reading and I apologize for any typos, unclear sections, and overall scuffedness.

Tags sorting algorithms, c++11, c++17

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en19 English willy108 2021-03-09 02:39:23 0 (published)
en18 English willy108 2021-03-09 02:39:10 122 (saved to drafts)
en17 English willy108 2021-03-09 02:33:57 0 (published)
en16 English willy108 2021-03-09 02:33:46 69 (saved to drafts)
en15 English willy108 2021-03-09 02:32:39 0 (published)
en14 English willy108 2021-03-09 02:30:51 4
en13 English willy108 2021-03-09 02:30:37 188
en12 English willy108 2021-03-09 02:29:08 11 Tiny change: 'etween them are the s' -> 'etween the submissions are the s'
en11 English willy108 2021-03-09 02:28:35 248
en10 English willy108 2021-03-09 02:26:29 69
en9 English willy108 2021-03-09 02:25:53 2 Tiny change: 'late to:\n- 1154ms' -> 'late to:\n\n- 1154ms'
en8 English willy108 2021-03-09 02:25:21 8
en7 English willy108 2021-03-09 02:25:04 12
en6 English willy108 2021-03-09 02:23:44 344
en5 English willy108 2021-03-09 02:18:49 120
en4 English willy108 2021-03-09 02:17:37 172
en3 English willy108 2021-03-09 02:14:14 31 Tiny change: 'st one. \n\nThe reso' -> 'st one. \n===============================\nThe reso'
en2 English willy108 2021-03-09 02:13:56 695
en1 English willy108 2021-03-09 02:07:37 1479 Initial revision (saved to drafts)