Data structure Application Standard Template Library stl--priority Queue (priority queue sort)

Source: Internet
Author: User
The abstract model of the priority queue does not consider the data storage structure as a sequential container. The element has no restrictions when it enters the priority queue, but has a standard when exiting. We can think of the priority queue as a collection of data elements, as if the elements were thrown into the trash, with little connection to each other. Contact the data like a hand in the bucket, pick a priority of the largest out.

The priority queue ADT interface is almost identical to the stack and queue container. However, in order to determine the priority size, the data type T must implement the relational operator <. The push () operation adds a data to the priority queue, and the pop () operation deletes the highest-priority data. As with other containers, we can also access the highest-priority state through the size () and empty () functions. In particular, we should note how the priority queue refers to the next data that will be ejected.  As with the stack, the priority queue uses the top () operation, in contrast, the queue is implemented through the front () operation. The Priority_queue class is in the header file queue.

By default, the priority queue assumes that the element with the largest value also has the highest precedence. That is, if x and Y are elements in the priority queue and x<y, then Y has a greater precedence than X.

The heap is a special two-fork tree, and it is very efficient to use heaps to implement priority queues. The STL Priority_queue class is implemented by using a heap to implement a priority queue.

The priority queue provides a very efficient sorting algorithm. The algorithm first joins the N=v.size () elements of vector v into a priority queue, and then jumps out the data one at a time through a loop from the priority queue.

Because the priority queue is in reverse order (from the highest priority to the lowest priority) when releasing the data, the 1th element that pops up the priority queue is stored in v[n-1] in the sort process, and the 2nd element is stored as v[n-2], and so on. This is done in ascending order. The following is the sorted code


#include <iostream>
#include <queue>//in order to use Priority_queue
#include <vector>  // In order to use the vector using
namespace std;
Template <typename t>
void Writevector (const VECTOR<T>&V)//output function requires only one constant reference vector parameter
{
	int i,n= V.size ();//capture vector size in n for
	(i=0;i<n;i++)
		cout<<v[i]<< "";
	cout<<endl;
}
Template<typename t>//using Priority queue template <typename t> to v sort
void Psort (VECTOR<T>&V)//function Psort () The sorting algorithm of this generic type T is implemented, and the generic type T must support the relational operator <.
{
	priority_queue<t> p;
	int i,n=v.size ();
	for (i=0;i<n;i++)
		P.push (V[i]);//Insert the element of vector v into the priority queue for
	(i=n-1;i>=0;i--)
	{
		v[i]=p.top ();  Remove elements from the priority queue and copy them back to Vector v
		p.pop ();}
}
int main ()
{
	int arr[]={16,18,20,48,50,64,35,77,23};
	int arrsize=sizeof (arr)/sizeof (int);
	Vector<int > V (arr,arr+arrsize);  Using the array initialization vector
	cout<< "Initial vector:";
	Writevector (v);

	Psort (v);

	cout<< "Sorted vector:";
	Writevector (v);
	return 0;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.