I need to create a method which sorts an array of integers using a Stack and a Queue. For instance if given [-1, 7, 0, 3, -2] -> [-2, -1, 0, 3, 7]. I'm completely lost at how to do this question, because I would just use a sorting method, however for this question I am not allowed to do that. Can anyone explain how to do this with a Stack and a Queue?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- How to toggle on Order in ReactJS
- StackExchange API - Deserialize Date in JSON Respo
To simply sort the
array
you can useArrays.sort()
.For queue specifically,
PriorityQueue
is what you need to sort theQueue
. Have a look at the java docs.You can insert the values in the
PriorityQueue
using theadd()
method and you will get a sortedQueue
back. You can also control the sorting order by using aComparator
.Many fast sorting algorithms (for example mergesort and quicksort) can be implemented efficiently on linked lists by using the fact that you can efficiently treat a singly-linked list as a stack (by prepending elements to the front) or queue (by appending elements to the back). Consequently, one possible way to solve this problem would be to take one of those sorting algorithms and approach it as if you were sorting a linked list rather than a normal sequence.
For example, here is one simple way that you could implement mergesort using queues. I've written this to sort
Integer
s, but this could easily be extended to handle other types:Overall, this will run in O(n log n) time, which is asymptotically optimal.
Alternatively, you could use quicksort, as shown here:
This runs in best-case O(n log n) time and worst-case O(n2) time. For an interesting exercise, try making it pick the pivot at random to get expected O(n log n) runtime. :-)
For an entirely different approach, you could consider doing a least-significant digit radix sort on the values, since you know that they're all integers:
This will run in time O(n log U), where U is the maximum possible value that can be stored in an
int
.Of course, all of these algorithms are efficient and elegant. Sometimes, though, you will want to do a slow and inelegant sort like bogosort. Now, bogosort is somewhat difficult to implement, since it typically requires you to shuffle the input sequence, which is way easier to do on an array. However, we can simulate shuffling a queue as follows:
This ends up taking time O(n2) rather than O(n), which has the unfortunate side-effect of making bogosort take expected time O(n2 &mdot; n!) rather than O(n &mdot; n!). However, it's the price we have to pay.
Hope this helps!
Stacks and Queues are data structures. Stack is a FILO and a Queue is FIFO. If you understand how those data structures work, you will be able to figure this out. Ultimately you can use the same logic that you would normally use for this type of problem, except you need to use stacks and queues as you data structures.
One way to use a stack in Java would be to sort the numbers with a recursive method, since Java has an internal memory stack.
Hope that helps.
You can implement a Selection sort fairly easily with a Stack and Queue:
If the data is originally on the stack, put it all on the Queue, and do the following:
After the sort is over the elements will be sorted on the Stack, removing them all will return data in order Greatest to Least (which can easily be reversed).
No this is not very efficient, i'm assuming it's a homework assignment. Realisticaly if you want to sort data you should use Arrays.sort or Collections.sort which implement a merge sort for objects and a quicksort for primitives. These will be far faster (O(NlogN) vs O(N*N)). Realize however you'll need to have data in an array or a list to do this, not a stack or queue.