std::stable_sort vs std::sort

Правка en10, от willy108, 2021-03-09 02:26:29

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.

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. Thanks for reading and I apologize for any typos, unclear sections, and overall scuffedness.

Теги sorting algorithms, c++11, c++17

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en19 Английский willy108 2021-03-09 02:39:23 0 (published)
en18 Английский willy108 2021-03-09 02:39:10 122 (saved to drafts)
en17 Английский willy108 2021-03-09 02:33:57 0 (published)
en16 Английский willy108 2021-03-09 02:33:46 69 (saved to drafts)
en15 Английский willy108 2021-03-09 02:32:39 0 (published)
en14 Английский willy108 2021-03-09 02:30:51 4
en13 Английский willy108 2021-03-09 02:30:37 188
en12 Английский willy108 2021-03-09 02:29:08 11 Tiny change: 'etween them are the s' -> 'etween the submissions are the s'
en11 Английский willy108 2021-03-09 02:28:35 248
en10 Английский willy108 2021-03-09 02:26:29 69
en9 Английский willy108 2021-03-09 02:25:53 2 Tiny change: 'late to:\n- 1154ms' -> 'late to:\n\n- 1154ms'
en8 Английский willy108 2021-03-09 02:25:21 8
en7 Английский willy108 2021-03-09 02:25:04 12
en6 Английский willy108 2021-03-09 02:23:44 344
en5 Английский willy108 2021-03-09 02:18:49 120
en4 Английский willy108 2021-03-09 02:17:37 172
en3 Английский willy108 2021-03-09 02:14:14 31 Tiny change: 'st one. \n\nThe reso' -> 'st one. \n===============================\nThe reso'
en2 Английский willy108 2021-03-09 02:13:56 695
en1 Английский willy108 2021-03-09 02:07:37 1479 Initial revision (saved to drafts)