You are not logged in. Please login at to post your questions!


Why does using a comparator function in sort make this program much slow ?

This gives TLE

bool mod(int a,int b){ return a>=b; } sort(a,a+n,mod);

Whereas , this runs in 0.06 sec , why ? Does using a cmp fn make sort too slow or should i use pass by reference or something like that ?

sort(a , a+n , greater<int>());

Only difference between the codes is the use of comparator. What can be the reason for slowdown ?

asked 15 Oct, 18:38

niteshx2's gravatar image

accept rate: 0%

= and <= is invalid in comparator functions. Change that into > and you shall make it. For every comparator function cmp(a, a) = 0 is a must


answered 15 Oct, 19:27

ducpro's gravatar image

accept rate: 0%

I'm not sure , what do we do then if we need stable sorting , and why did the code run if the comparator was invalid ? Thanks.

(15 Oct, 22:50) niteshx24★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 15 Oct, 18:38

question was seen: 89 times

last updated: 15 Oct, 22:50