📜  初始化优先队列的有效方法

📅  最后修改于: 2021-09-06 06:03:10             🧑  作者: Mango

STL Priority Queue 是堆数据结构的实现。默认情况下,它是最大堆,并且可以轻松用于原始数据类型。在本文中可以找到它的一些重要应用。

优先队列可以通过两种方式初始化,一种是将所有元素一一推送,另一种是使用它们的构造函数进行初始化。在本文中,我们将讨论这两种方法并检查它们的时间复杂度。

方法一:最简单的方法就是遍历给定的数组,将每个元素一一压入优先级队列。在这个方法中,优先队列中的push方法需要O(log N)的时间。其中N是数组中的元素数。

下面是上述方法的实现:

C++
// C++ program to initialize the
// priority queue
#include 
using namespace std;
 
// Driver Code
int main()
{
    int arr[] = { 15, 25, 6, 54, 45, 26, 12 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Initialize priority_queue
    priority_queue pq;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // Push the element arr[i]
        pq.push(arr[i]);
    }
 
    cout << "The elements in priority"
         << " Queue are: ";
 
    // Traverse until pq is non-empty
    while (!pq.empty()) {
 
        // Print the element in pq
        cout << pq.top() << " ";
 
        // Pop the top element
        pq.pop();
    }
 
    return 0;
}


Java
// Java program to initialize the
// priority queue
import java.util.*;
public class GFG
{
    public static void main(String[] args)
    {
        int[] arr = { 15, 25, 6, 54, 45, 26, 12 };
        int N = arr.length;
      
        // Initialize priority_queue
        Vector pq = new Vector();
      
        // Traverse the array arr[]
        for (int i = 0; i < N; i++)
        {
      
          // Push the element arr[i]
          pq.add(arr[i]);
        }
        Collections.sort(pq);
        Collections.reverse(pq);
        System.out.print("The elements in priority" + " Queue are: ");
      
        // Traverse until pq is non-empty
        while (pq.size() > 0)
        {
      
          // Print the element in pq
          System.out.print(pq.get(0) + " ");
      
          // Pop the top element
          pq.remove(0);
        }
    }
}
 
// This code is contributed by divyesh072019.


Python3
# Python3 program to initialize the
# priority queue
 
# Driver Code
if __name__ == '__main__':
    arr = [15, 25, 6, 54, 45, 26, 12]
    N = len(arr)
 
    # Initialize priority_queue
    pq = []
 
    # Traverse the array arr[]
    for i in range(N):
       
        # Push the element arr[i]
        pq.append(arr[i])
    print("The elements in priority Queue are: ", end = "")
    pq = sorted(pq)
 
    # Traverse until pq is non-empty
    while (len(pq) > 0):
 
        # Print the element in pq
        print(pq[-1], end = " ")
 
        # Pop the top element
        del pq[-1]
 
        # This code is contributed by mohit kumar 29.


C#
// C# program to initialize the
// priority queue
using System;
using System.Collections.Generic;
class GfG
{
  public static void Main()
  {
    int[] arr = { 15, 25, 6, 54, 45, 26, 12 };
    int N = arr.Length;
 
    // Initialize priority_queue
    List pq = new List();
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
      // Push the element arr[i]
      pq.Add(arr[i]);
    }
 
    pq.Sort();
    pq.Reverse();
 
    Console.Write("The elements in priority" + " Queue are: ");
 
    // Traverse until pq is non-empty
    while (pq.Count > 0) {
 
      // Print the element in pq
      Console.Write(pq[0] + " ");
 
      // Pop the top element
      pq.RemoveAt(0);
    }
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


C++
// C++ program to initialize the
// priority queue
#include 
#include 
using namespace std;
 
// Driver Code
int main()
{
    int arr[] = { 15, 25, 6, 54, 45, 26, 12 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // By this type of initialization
    // the priority_queue is using
    // build heap to make the max heap
    cout << "The elements in priority"
         << " Queue are: ";
 
    // Initialize priority_queue
    priority_queue pq(arr, arr + N);
 
    // Iterate until pq is non empty
    while (!pq.empty()) {
 
        // Print the element
        cout << pq.top() << " ";
        pq.pop();
    }
 
    return 0;
}


Python3
# Python3 program to initialize the
# priority queue
 
# Driver Code
if __name__=='__main__':
     
    arr = [ 15, 25, 6, 54, 45, 26, 12 ]
    N = len(arr)
     
    # By this type of initialization
    # the priority_queue is using
    # build heap to make the max heap
    print("The elements in priority Queue are: ", end = '')
     
    # Initialize priority_queue
    pq = arr
    pq.sort()
 
    # Iterate until pq is non empty
    while (len(pq) != 0):
       
        # Print the element
        print(pq[-1], end = ' ')
        pq.pop()
     
    # This code is contributed by rutvik_56.


输出:

The elements in priority Queue are: 54 45 26 25 15 12 6

时间复杂度: O(N*log N)
辅助空间: O(N)

方法二:在该方法中,在初始化优先级队列时,将所有数组元素复制到优先级队列中(这种复制将使用priority_queue的复制构造函数进行)。在此方法中, priority_queue将在内部使用构建堆方法。所以构建堆方法需要O(N)时间。

句法:

数组的语法

向量的语法

下面是上述方法的实现:

C++

// C++ program to initialize the
// priority queue
#include 
#include 
using namespace std;
 
// Driver Code
int main()
{
    int arr[] = { 15, 25, 6, 54, 45, 26, 12 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // By this type of initialization
    // the priority_queue is using
    // build heap to make the max heap
    cout << "The elements in priority"
         << " Queue are: ";
 
    // Initialize priority_queue
    priority_queue pq(arr, arr + N);
 
    // Iterate until pq is non empty
    while (!pq.empty()) {
 
        // Print the element
        cout << pq.top() << " ";
        pq.pop();
    }
 
    return 0;
}

蟒蛇3

# Python3 program to initialize the
# priority queue
 
# Driver Code
if __name__=='__main__':
     
    arr = [ 15, 25, 6, 54, 45, 26, 12 ]
    N = len(arr)
     
    # By this type of initialization
    # the priority_queue is using
    # build heap to make the max heap
    print("The elements in priority Queue are: ", end = '')
     
    # Initialize priority_queue
    pq = arr
    pq.sort()
 
    # Iterate until pq is non empty
    while (len(pq) != 0):
       
        # Print the element
        print(pq[-1], end = ' ')
        pq.pop()
     
    # This code is contributed by rutvik_56.
输出:
The elements in priority Queue are: 54 45 26 25 15 12 6

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live