In this example
return A.age < B.age gives output in decreasing order but in sort(a,a+n,compare) return a<b gives an ascending order. Why is the happening?
Comparators doubt
@avijuneja2007 priority queus are different than regular arrays, do you have any confusion regarding that? If yes please let me know
Yes I know that priority queues are different than arrays! Prateek sir said that in sort function we pass compare as a parameter whereas in priority queue we pass it as comparator class. So basically why is there a difference in the result?
@avijuneja2007 if a priority queue is functioning like a max heap, then the maximum element will be present at the root. Hence if you keep popping elements from root and print them, they’ll come out in descending order. You can turn it into a max or min heap with the help of the comparator class.
Yes I totally understand ur point. But suppose if we make a min heap and pass compare class in which we return A.age < B.age, then why is it giving output in decreasing order like in this case it is giving
40
20
15
10
3
@avijuneja2007 if you want to implement a min heap you have to return A.age > B.age
You can refer to this article https://www.geeksforgeeks.org/implement-min-heap-using-stl/
So why is return A.age < B.age giving output in descending order although it gives the output in ascending order in case of arays.
@avijuneja2007 they are different data structures that are implemented differently, so you have to do things differently for both of them. Any other container besides priority queues works like arrays only (like vectors, strings etc) so they’d give you a result like array only. But priority queues are not normal sequential containers.