views:

201

answers:

6

I want to know why we always use Sorting algorithm like (Insertion Sort or Merge Sort,...) just for lists and arrays? And why we do not use these algorithms for stack or queue?

+5  A: 

That's because the order of a stack or a queue is part of the definition of these data structures. If we sorted them, they wouldn't be stacks or queues.

Peter Jaric
OK but I can save their order (FIFO and LIFO) and sorting them with maybe more than 2 stacks or queues can I do?
I am not sure I follow you, but regardless: there is no point in sorting a stack, since its order is important.
Peter Jaric
+2  A: 

A Stack or Queue are concepts which differ from Sequences. Arrays and Linked Lists represent Sequences, on which you can think of them as sorted or unsorted.

AraK
+11  A: 

Stacks and queues are abstract data types that have their own sense of order, i.e. LIFO (Last In First Out) for stacks and FIFO (First In First Out) for queues. As such, it does not make sense to take a queue/stack and reorder their elements around.

Wikipedia references


On Stack vs Vector

You may notice that in Java, java.util.Stackextendsjava.util.Vector, and since it makes sense to sort a Vector, perhaps it also makes sense to sort a Stack. This is not the case however; the fact that Stack extends Vector is in fact a design blunder. A stack is NOT a vector.

Related questions


On using Collections.sort on java.util.Stack

Despite the fact that it doesn't make sense to use, say, quicksort on a stack, you CAN actually use Collections.sort on a java.util.Stack. Why? Because, by virtue of design error (this can't be emphasized enough!), a java.util.Stack is a java.util.Vector, which implements java.util.List, and you certainly can sort a List. Here's an example:

    Stack<Integer> stack = new Stack<Integer>();
    stack.push(1);
    stack.push(3);
    stack.push(5);
    stack.push(2);
    stack.push(4);

    Collections.sort(stack); // by virtue of design error!!!

    System.out.println(stack); // prints "[1, 2, 3, 4, 5]"
    while (!stack.isEmpty()) {
        System.out.println(stack.pop());
    } // prints "5", "4", "3", "2", "1"

Note that the elements are printed in descending order: this is because of how java.util.Stack is implemented. It pushes to and pops from the end of the Vector. You don't need to know this; you shouldn't have known this; but these are the facts.


On using an appropriate data structure

Depending on what it is that you're trying to accomplish, a TreeSet may be the appropriate data structure. It is a Set, so it does not permit duplicate elements.

    NavigableSet<Integer> nums = new TreeSet<Integer>();
    nums.add(5);
    nums.add(3);
    nums.add(1);
    nums.add(2);
    nums.add(6);

    System.out.println(nums.pollFirst()); // prints "1"
    System.out.println(nums.pollFirst()); // prints "2"
    nums.add(4);
    System.out.println(nums.pollFirst()); // prints "3"
    System.out.println(nums.pollFirst()); // prints "4"
polygenelubricants
+1 for the additional comment about Stack and Vector.
Peter Jaric
Consider this stack A =(2,7,3,9,1,5) why it doesn't make sense to reorder them like A=(1,2,3,5,7,9)??
Because on a stack you expect to get the last pushed element when you pop an element. But in your example (assuming pushed elements are put at the beginning) after pushing 2 and then sorting, you'd get 1 when you pop.
Peter Jaric
@matin1234: you can certainly make a `List` out of the elements of a stack and reorder them however you wish. To reorder the elements of the stack itself, however, violates the very definition of what a stack is and how it should behave.
polygenelubricants
for example I want to have a sorted data structure that I can add or remove data just from its first ,so which data structure should I use?isn't a sorted stack?
Stack extending Vector is a non-issue. The extra functions inherited from Vector don't allow you to do anything you couldn't otherwise do with a sequence of pops and pushes (sorting included). Contrast with the unending 'is a Circle an Ellipse' debates.
CurtainDog
@ polygenelubricants: aha you mean that at first pop all the stack elements and add them in to the list and sort them in the list then push all them back to the first stack??
Furthermore, one of the most obvious (to the layman) references to the stack structure is the old Towers of Hanoi problem, which, by the nature of the constraints, is effectively a set of sorted stacks.
CurtainDog
@matin1234: You can do that, or you can use a `TreeSet` with its `pollFirst`. Or you can actually `Collections.sort` a `Stack` (since it's a `Vector`, which is a `List`), but that's further abusing the fact that `Stack extends Vector` is a design error in the first place.
polygenelubricants
aha now I get a little I should study TreeSet!! thanks any way
sorry for your stack example why it return "5","4",... I think it should return "1","2",... Isn't?
@matin1234: Like I said, a stack pops to and push from the end, not the beginning of the vector. If you need 1,2,... then sort into descending order. Confusing, isn't it? That's because you're not supposed to use a stack this way.
polygenelubricants
yes you are right ,I confused !! so if reordering the stack or queue is not sensible so why we sometimes pass stack as an argument in to the Collections.sort() method?
@matin1234: No, you don't "sometimes" do this. You _can_ do this because of an error in design. You _shouldn't_ do this. There's a lot of issues here to digest, but the ultimate truth is that a stack is not supposed to be sortable. You _can_ do it one way or another, but you're really mistreating the stack at that point.
polygenelubricants
aha,thanks I get the whole ,really thanks .you always help me and I get my answers with your nice answers!![:-)]
+3  A: 

As others noted, in general it doesn't make sense to order stacks and queues. Just to make the picture full, there is an exception: PriorityQueue, which keeps its elements ordered.

Péter Török
+1  A: 

Stack and Queue have their own unique structure. Stack is a structure that applies Last In First Out(LIFO). If you ordered a Stack, then it would violate LIFO. Think that I add 7, 3, 5, 4 to a stack. As you know, stack can only retrieve the last added element. Whenever, we call pop() method, it will retrieve 4. However, think that you now sort it. It becomes 3, 4, 5, 7 and when we pop(), it will retrieve 7 which was the first element that we added. This violates LIFO rule.

The same is valid for Queue, because Queue structure applies First in First Out. If you have any question, please don't hesitate to ask.

Mikae Combarado
A: 

First of all Stack and Queues are also list but having some special characteristics. Since they are list you can always sort them but if you do that you might alter properties of these data structures.

Then there will be no point to using these data structure throughout your code and at some point sort them to loose their property for which we were using them.

GG