ShahariarIslam's blog

By ShahariarIslam, history, 8 months ago, In English

sort(v.begin(),v.end(),greater ()); OR sort(v.rbegin(),v.rend()); OR sort(v.begin(),v.end()); reverse(v.begin(),v.end()); All these are the same

  • Vote: I like it
  • -12
  • Vote: I do not like it

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ShahariarIslam (previous revision, new revision, compare).

»
8 months ago, # |
  Vote: I like it +1 Vote: I do not like it

ok but i perfer sort(v.rbegin(), v.rend());

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ShahariarIslam (previous revision, new revision, compare).

»
6 months ago, # |
  Vote: I like it +3 Vote: I do not like it

seek help

»
6 months ago, # |
  Vote: I like it +11 Vote: I do not like it

They all have a time complexity O(n log n), in the sorting process the first one uses the greater<int>() comparator to sort in descending order based on this criterion while the second one uses reverse iterators instead of the comparator. But the third one actually adds a small constant factor because of the reversing, making the time complexity combined of O(n log n) + O(n) which is still O(n log n) overall but makes the first and the second methods slightly more efficient than it