priority queue based on an unsorted sequence

Any idea or example of a Java implementation of a priority queue based on an unsorted sequence?
Who is Participating?
CEHJConnect With a Mentor Commented:
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));
            for (int i = 0; i < 10; i++) {
                  System.out.println("current head is: " + PQ.poll());
fffej78Connect With a Mentor Commented:
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.

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.