shilpa Reddy
Ranch Hand
Posts: 42

This code produces something like this:
1 3 5 6 7 8 9
size 7
peek 9
size 7
poll 9
size 6
8 7 6 5 3 1 null

i dont understand the comparator part of it.....
a) wat does this code do:

b)
}
}

how does this do a inverse sort.....what is taken in as integer one and integer two.......i understand that it takes in the given array....but hoe does it sort.....how do they get it as 9 8 7 6 5 3 1....can anyone explain me clearly.....

nothing is goin in to my head!!!(have got my exam this month ).can anyone gimme a link where the minute tips and traps for the exam should be read......

shilpa

[ August 16, 2006: Message edited by: shilpa Reddy ]
[ August 16, 2006: Message edited by: shilpa Reddy ]

Barry Gaunt
Ranch Hand
Posts: 7729
If you wish that it is more likely for people to read your code, then please place correctly formatted code between tags.

You can edit your original post by selecting the edit icon (the one like a piece of notepaper and pencil)

Stephen O'Kane
Greenhorn
Posts: 26
It's all in the API if you care to look at it:

PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

The static class PQsort will take two Integer objects and compare them, not according to natural order (which is default for a PriorityQueue), but to reverse natural order (hence return two - one; if it was return one - two; then it would still be natural order).

When a PriorityQueue is created with a Comparator, then it will use the comparator to decide where in the Queue the value should go. If it is created with no Comparator, it uses natural order

shilpa Reddy
Ranch Hand
Posts: 42
Thanks a lot for tha explanation....so here the initial capacity is 10... wat is the significance of it......does it mean that the priority queue will contain 10 elements......i understand that it is reverse sorted order.....but i dont understand how it happens....can you explain that basic thing...wat would go in as the Interger 1 and Integer 2....how do they compare and how does return two-one give you 9 8 7......can you please explain.....

[ August 16, 2006: Message edited by: shilpa Reddy ]
[ August 16, 2006: Message edited by: shilpa Reddy ]

Moieen Khatri
Ranch Hand
Posts: 144
Hi,

Please can someone explain how the below code works and inverse sorting happens:

How are the 2 parameters Integer one and Integer two are passed to the compare method and how the difference between the 2 parameters does the sorting..I am really confused.. Please help

Ranch Hand
Posts: 59
Hi Moieen and Shilpa,
According to what I have understood from API.

When we give a comparator type instance To priority queue constructor, its elements are sorted in the order in which the compare method is implemented. Here when we are calling constructor of pq2 , we are providing it with pqs. The compare method implemented here works like this.
Lets say we have given the queue the elements 1 and then 5
The compare method will execute for lets say(1,5) and return o/p >0
if o/p>0 then one is greater than two
if o/p<0 then one is lesser that two
if o/p==0 then one is equal to two.

and will place elements in order 5,1