[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
Medium Priority
385 Views
Any idea or example of a Java implementation of a priority queue based on an unsorted sequence?
0
Question by:dbkattel

LVL 86

Accepted Solution

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++) {
}
System.out.println(PQ);
for (int i = 0; i < 10; i++) {
System.out.println("current head is: " + PQ.poll());
}
}
}
0

LVL 14

Assisted Solution

hoomanv earned 500 total points
ID: 16775908
0

LVL 92

Assisted Solution

objects earned 500 total points
ID: 16777312
0

LVL 4

Assisted Solution

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

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

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

thanks.
0

## Featured Post

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