473,480 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

PriorityQueue not working properly

48 New Member
I have made a priorityqueue which inserts objects and compares them with cost parameter ,when two costs are equal it should keep them in enqueued order,but i found after debugging one time it is in enqueued order and other time it is not in the order but i am not getting what is wrong with my code

Expand|Select|Wrap|Line Numbers
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class node implements Comparable < node > {
  8.  
  9.   int x;
  10.   int y;
  11.   int dir;
  12.  
  13.   node(int x, int y, int dir) {
  14.     this.x = x;
  15.     this.y = y;
  16.     this.dir = dir;
  17.   }
  18.  
  19.   public int compareTo(node o) {
  20.  
  21.     if (Ideone.cost[o.x][o.y] == Ideone.cost[x][y])
  22.       return 1;
  23.     else {
  24.       int d = Ideone.cost[x][y] - Ideone.cost[o.x][o.y];
  25.       if (d > 0)
  26.         return 1;
  27.       else
  28.         return -1;
  29.     }
  30.   }
  31.  
  32. }
  33.  
  34. /* Name of the class has to be "Main" only if the class is public. */
  35. class Ideone {
  36.  
  37.   public static int[][] cost;
  38.   static PriorityQueue < node > p;
  39.   public static void main(String[] args) throws java.lang.Exception {
  40.  
  41.     p = new PriorityQueue < node > ();
  42.     cost = new int[13][11];
  43.  
  44.     for (int[] row: cost)
  45.       Arrays.fill(row, -1);
  46.     cost[0][8] = 366564;
  47.     cost[2][9] = 368282;
  48.     cost[1][3] = 368282;
  49.     cost[4][9] = 368282;
  50.     cost[0][9] = 376564;
  51.     cost[1][9] = 372423;
  52.     cost[5][9] = 372423;
  53.     cost[0][3] = 436564;
  54.     cost[7][0] = 378282;
  55.     cost[2][10] = 378282;
  56.     cost[4][10] = 378282;
  57.     cost[0][4] = 382423;
  58.     p.add(new node(0, 8, 8));
  59.     p.add(new node(2, 9, 8));
  60.     p.add(new node(1, 3, 7));
  61.     p.add(new node(4, 9, 2));
  62.     p.add(new node(0, 9, 8));
  63.     p.add(new node(1, 9, 8));
  64.     p.add(new node(5, 9, 2));
  65.     p.add(new node(0, 3, 6));
  66.     p.add(new node(7, 0, 3));
  67.     p.add(new node(2, 10, 8));
  68.     p.add(new node(4, 10, 2));
  69.     p.add(new node(0, 4, 7));
  70.  
  71.     while (p.size() != 0) {
  72.       node n1 = p.poll();
  73.       System.out.println(n1.x + " " + n1.y + " " + cost[n1.x][n1.y]);
  74.  
  75.     }
  76.  
  77.   }
  78. }
  79.  
Output is

0 8 366564
1 3 368282
2 9 368282
4 9 368282
5 9 372423
1 9 372423
0 9 376564
4 10 378282
2 10 378282
7 0 378282
0 4 382423
0 3 436564

but i am expecting

0 8 366564
2 9 368282
1 3 368282
4 9 368282
1 9 372423
5 9 372423
0 9 376564
7 0 378282
2 10 378282
4 10 378282
0 4 382423
0 3 436564
Sep 6 '16 #1
0 975

Sign in to post your reply or Sign up for a free account.

Similar topics

8
1978
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion is showing half the range of farenheit. print...
1
3290
by: Roberto Castro | last post by:
I have some problems with the way I am showing the BLOB fields in the Image web controls. It does work on my localhost though sometimes I need to hit Refresh for the images to load properly....
5
2774
by: Nita Raju | last post by:
Hi, I have to validate a textbox for date without using the validation controls. So i had to use IsDate(). It's not working properly when i give "11//2004". When i enter the above date it...
2
3269
by: Annu | last post by:
Hi I need help on <enbed> tag. Following code(No 1) is working properly on windows but on linux code no.2 is not working Code No 1: <EMBED type='application/x-mplayer2' ...
12
4559
kamill
by: kamill | last post by:
I have done a logout page for logout from admin section and provides a link to logout from admin section.Whenever i clicked on logout link it redirected to index.php of admin section......BUT when i...
5
3599
by: damezumari | last post by:
When a user logs in to my site http://iwantyourquestion.com I set $_SESSION to true if his username and password are OK. When he calls a page I check if $_SESSION is true. If it not I ask him to...
3
1993
by: rajasree | last post by:
Hi all, am doing a project in PHP. my javascript code is working properly in ie. But its not working in firefox. Please help me my code is as follows; <script language="javascript"...
4
3741
by: zairali | last post by:
Hi, I am trying to fix a webpage ( http://www.d.umn.edu/itss/labs/maps/ ) which uses some html (or xml also?) to show pop ups when you rollover the numbers on a map of the building. They work fine...
8
3355
by: neovantage | last post by:
Hey all, Website page is not working properly in IE6 and IE7 but working normal in firefox. Can somebody tell me how i can fixed that problem. Here is the url of the page http://www.ragehockey.biz/...
1
3011
by: Jiwei06xie | last post by:
Hi, I have a main form with 3 subforms and some combo boxes. It is working fine as an independent form. However, when I draged into a tabcontrol page, two off the combo boxes stopped working...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.