[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

priority queue based on an unsorted sequence

Posted on 2006-05-27
7
Medium Priority
?
385 Views
Last Modified: 2012-06-27
Any idea or example of a Java implementation of a priority queue based on an unsorted sequence?
0
Comment
Question by:dbkattel
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 16775831
This shows the queue, starting unordered, then how it orders itself:

/**
 * 27-May-2006
 */

package misc;

/**
 * @author Charles
 */

import java.util.PriorityQueue;

public class PriorityQueueExample {

      public static void main(String args[]) {
            PriorityQueue<Integer> PQ = new PriorityQueue<Integer>();
            for (int i = 0; i < 10; i++) {
                  PQ.add((int)(Math.random() * 100));
            }
            System.out.println(PQ);
            for (int i = 0; i < 10; i++) {
                  System.out.println("current head is: " + PQ.poll());
            }
      }
}
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 500 total points
ID: 16775908
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 16777312
0
 
LVL 4

Assisted Solution

by:fffej78
fffej78 earned 500 total points
ID: 16824179
A priority queue is an abstract data type with the following simple set of operations:

add - adds a new element
findMinimum or findMaximum - find the largest / smallest element
deleteMinimum or deleteMaximum - remove the largest / smallest element

You can easily implement this on top of a sequence.  As a simple example, you could just have a sequence underneath (completely unsorted, I presume that is what you mean by an unordered sequence).

Adding an element would add to the end / beginning of a list, this would be either O(1) or O(n)

Finding an element (and deleting an element) would be O(n) as you'd have to scan every element to find the right one.  This is the downside of not maintaining a sorted list.

Given how inefficient this is, people tend to use a more sophisticated data structure, such as a heap or maintain a sorted list.

0
 

Author Comment

by:dbkattel
ID: 17017434
i am still looking forward for an appropriate solution (a working java program) for this problem.

thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question