You are not logged in. Please login at www.codechef.com 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

4★niteshx2
414
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

link

answered 15 Oct, 19:27

ducpro's gravatar image

6★ducpro
24017
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
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×770
×74
×21

question asked: 15 Oct, 18:38

question was seen: 89 times

last updated: 15 Oct, 22:50