This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparator-someone answer this

 
shilpa Reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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......

Thanks in advance
shilpa

[ August 16, 2006: Message edited by: shilpa Reddy ]
[ August 16, 2006: Message edited by: shilpa Reddy ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Thanks in advance
 
Charmy Madhvani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Then we add 3 (5,1,3)

Then it will execute for (1,3) and return o/p >0
and will place elements in order 5,3,1

and so on.. If you go on like this way for each combination you will definitely get a reverse sort of entire elements.
I assume this is done internally within priority queue.
When and where even I dont know.(May be while loading elements)..

I dont know whether this is a correct explanation.
But this is what I have concluded..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic