Priority Queue Implementation using Array

Priority Queue Implementation using Array

Priority Queue Implementation using Array in Java demonstrates how a priority queue can be implemented manually using arrays. In a priority queue, elements are removed based on priority rather than insertion order. The element with the highest priority is processed first.

While Java provides a built in PriorityQueue class in the Java Collections Framework, understanding how to implement it using arrays helps you learn the internal working of priority based data structures.

priority queue using array in java

Priority Queue using Array in Java

Every element in the priority queue is associated with a priority. It does not matter in which order we insert the items in the queue, the item with higher priority must be removed before the item with the lower priority.

If the two items have same priorities, the order of removal is not defined and depends on implementation

In an array based priority queue:

  • Elements are stored in an array
  • The element with the highest priority is located during deletion
  • Elements may be rearranged to maintain priority order

There are 2 common approaches:

  1. Unsorted Array Implementation
  2. Sorted Array Implementation

Operations on a Priority Queue

  1. EnQueue: EnQueue operation inserts an item into the queue. The item can be inserted at the end of the queue or at the front of the queue or at the middle. The item must have a priority.
  2. DeQueue: DeQueue operation removes the item with the highest priority from the queue.
  3. Peek: Peek operation reads the item with the highest priority.

Types of Priority Queue in Java

  • Min Priority Queue: In min priority Queue minimum number of value gets the highest priority and lowest number of element gets the highest priority.
  • Max Priority Queue: Max priority Queue is where  maximum number value gets the highest priority and minimum number of value gets the minimum priority.
Implementation of Priority Queue using Array

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Priority Queue Implementation using Array in Java

Priority Queue can be implemented in two ways:

  1. Using ordered Array: In ordered array insertion or enqueue operation takes O(n) time complexity because it enters elements in sorted order in queue. And deletion takes O(1) time complexity. 
  2. Using unordered Array: In unordered array deletion takes O(n) time complexity because it search for the element in Queue for the deletion and enqueue takes o(1) time complexity.

Method 1: Priority Queue using Unsorted Array

ALGORITHM:

1. Insert Element

1. If queue is full
        Print overflow
2. Insert element at rear
3. Increment size

2. Delete Highest Priority Element

1. If queue is empty
        Print underflow
2. Find element with highest priority
3. Remove it
4. Shift remaining elements

Java Code

Run
public class PriorityQueueArray {

    int[] arr;
    int size;
    int capacity;

    public PriorityQueueArray(int cap) {
        capacity = cap;
        arr = new int[capacity];
        size = 0;
    }

    // Insert element
    public void insert(int value) {

        if (size == capacity) {
            System.out.println("Queue Overflow");
            return;
        }

        arr[size++] = value;
        System.out.println(value + " inserted");
    }

    // Delete highest priority element (smallest value)
    public int delete() {

        if (size == 0) {
            System.out.println("Queue Underflow");
            return -1;
        }

        int highestPriorityIndex = 0;

        for (int i = 1; i < size; i++) {
            if (arr[i] < arr[highestPriorityIndex]) {
                highestPriorityIndex = i;
            }
        }

        int removed = arr[highestPriorityIndex];

        // Shift elements
        for (int i = highestPriorityIndex; i < size - 1; i++) {
            arr[i] = arr[i + 1];
        }

        size--;

        return removed;
    }

    // Display queue
    public void display() {

        if (size == 0) {
            System.out.println("Queue is empty");
            return;
        }

        System.out.print("Priority Queue: ");

        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }

        System.out.println();
    }

    public static void main(String[] args) {

        PriorityQueueArray pq = new PriorityQueueArray(5);

        pq.insert(30);
        pq.insert(10);
        pq.insert(50);
        pq.insert(20);

        pq.display();

        System.out.println("Removed: " + pq.delete());
        System.out.println("Removed: " + pq.delete());

        pq.display();
    }
}

Input:

Insert: 30
Insert: 10
Insert: 50
Insert: 20
Delete
Delete

Output:

30 inserted
10 inserted
50 inserted
20 inserted
Priority Queue: 30 10 50 20
Removed: 10
Removed: 20
Priority Queue: 30 50

Priority Queue Implementation using Array in Java

Elements are stored in sorted order based on priority:

  • Insert operation places element at correct position
  • Delete operation removes the first element

Method 2: Priority Queue using Sorted Array

ALGORITHM:

1. Insert Element

1. If queue is full
      Print overflow
2. Find correct position
3. Shift elements
4. Insert element

2. Delete Highest Priority Element

1. If queue empty
      Print underflow
2. Remove first element
3. Shift elements left

Java Code

Run
public class SortedPriorityQueue {

    int[] arr;
    int size;
    int capacity;

    public SortedPriorityQueue(int cap) {
        capacity = cap;
        arr = new int[capacity];
        size = 0;
    }

    public void insert(int value) {

        if (size == capacity) {
            System.out.println("Queue Overflow");
            return;
        }

        int i;

        for (i = size - 1; i >= 0 && arr[i] > value; i--) {
            arr[i + 1] = arr[i];
        }

        arr[i + 1] = value;
        size++;

        System.out.println(value + " inserted");
    }

    public int delete() {

        if (size == 0) {
            System.out.println("Queue Underflow");
            return -1;
        }

        return arr[--size];
    }

    public void display() {

        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }

        System.out.println();
    }

    public static void main(String[] args) {

        SortedPriorityQueue pq = new SortedPriorityQueue(5);

        pq.insert(30);
        pq.insert(10);
        pq.insert(50);
        pq.insert(20);

        pq.display();

        System.out.println("Removed: " + pq.delete());
        System.out.println("Removed: " + pq.delete());

        pq.display();
    }
}

Output:

10 inserted
20 inserted
30 inserted
50 inserted
10 20 30 50
Removed: 50
Removed: 30
10 20

Frequently Asked Questions

Answer:

It is a priority queue implementation where elements are stored in an array and removed based on priority.

Answer:

Insertion can be O(1) or O(n), while deletion can be O(n) or O(1) depending on implementation.

Answer:

Sorted array provides faster deletion, while unsorted array provides faster insertion.

Answer:

Yes, Java provides the PriorityQueue class in java.util.

Answer:

Heap provides O(log n) time for both insertion and deletion, making it more efficient.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java