answersLogoWhite

0

How do you sort a queue?

Updated: 8/11/2023
User Avatar

Wiki User

9y ago

Best Answer

You don't. Queues are a first in, first out structure, specifically used to process incoming data in the same order it arrives. If you want to sort a data sequence then use an array or a list.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Not to be pedantic, but if you merge and sort one queue into another queue then it is no longer a queue (first in, first out). The result is a sorted list. Regardless, there are a number of ways to merge and sort.

If both queues can themselves be sorted, then you can simply sort them and then traverse both queues simultaneously, extracting the smaller node of the two and inserting it into a new list. Continue in this fashion until both queues are empty.

If the queues cannot be sorted individually, create a temporary one-dimensional array of pointers to the data type, with enough elements to accommodate all nodes from both queues. Then traverse each queue and sequentially update the array with the data pointers. Then sort the array (do not compare pointers, compare what they point at, via indirection). Once the array is sorted, you can instantiate a new list and sequentially copy data indirectly from the array pointers. Note that both queues must remain constant during this entire process. Once the new, sorted list is built, you can delete the temporary array (but do not delete the pointers in the array).

Alternatively, if you have a list that can itself be sorted, you can forego using an array and simply copy data from the queues directly into the list, and then sort the list.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Data Mining in C++

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you sort a queue?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How queue is implemented?

quick sort am i correct?


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


What are types of Queue?

Queue is a data structure which is based on FIFO that is first in first out. Following are the types of queue: Linear queue Circular queue Priority queue Double ended queue ( or deque )


Which queue most efficient queue using array?

circular queue


What is difference between job queue and ready queue?

Ready queue contain all the jobs that are ready to execute.so the job queue and the ready queue are one and the same.


Queue in a sentence?

He added the download to the queue.(Line)I saw a queue in the park(waiting line)


What is linear queue?

What is linear queue


What is the difference between a priority queue and a circular queue?

A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. A priority queue is a queue in which each element is inserted or deleted on the basis of their priority. A higher priority element is added first before any lower priority element. If in case priority of two element is same then they are added to the queue on FCFS basis (first come first serve).


What is the collective noun for queue?

The noun 'queue' is a collective noun for a 'queue of people'.


What is queue explain the basic element of queue?

The queue is a linear data structure where operations of insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Following are the types of queue: Linear queue Circular queue Priority queue Double ended queue ( or deque )


What English word can have 4 of its 5 letters taken away and still retain its original pronunciation?

Queue or Q (as in waiting in a queue or the letter Q)


What is circular queue operations circular queue?

A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. Advantage of this type of queue is that empty location let due to deletion of elements using front pointer can again be filled using rear pointer. There are 2 conditions for queue full if queue is implemented using arrays. First condition is Front = 1 and Rear = N Second condition is Front = Rear + 1