tags:

views:

337

answers:

6

Dont you peaple think that quicksort is stable if follow following algo for partitioning the array:

  partition(A,p,r)
  {
     x=A[r];
     i=p-1;
     for j=p to r-1
       if(A[j]<=x)
          i++;
          exchange(A[i],A[j])

       exchang(A[i+1],A[r]); 
     return i+1;
   }

Regards!!

A: 

Try it! Do some experiments with tricky datasets and demonstrate it.

Alex Feinman
although an aggravating answer, I'm not sure it deserves the downvote.
Jeremy Powell
It depends on what you consider the purpose of Stack Overflow to be: learning, or getting answers.
Alex Feinman
+1  A: 

From Wikipedia:

Quicksort is a comparison sort and, in efficient implementations, is not a stable sort.

Matthew Jones
but note that it can be made stable (and less quick) with a proper partitioning algorithm and good choice of the pivot element.
jilles de wit
So it's pick-your-poison, less stability or reduced quickness.
Matthew Jones
can you plz give a set of test data which can show that following the above partioning scheme will led to loss stability in quicksort!
mawia
+1  A: 

Please fix the braces and indentation in your pseudocode. Also, your code looks suspiciously similar to the sample partition function given on wikipedia which isn't stable, so your function probably isn't stable. At the very least you should make sure your pivot point r points to the last position in the array of values equal to A[r].

You can make quicksort stable (I disagree with Matthew Jones there) but not in it's default and quickest (heh) form.

Martin (see the comments) is correct that a quicksort on a linked list where you start with the first element as pivot and append values at the end of the lower and upper sublists as you go through the array. However, quicksort is supposed to work on a simple array rather than a linked list. One of the advantages of quicksort is it's low memory footprint (because everything happens in place). If you're using a linked list you're already incurring a memory overhead for all the pointers to next values etc, and you're swapping those rather than the values.

jilles de wit
Can you make quicksort stable without changing the algorithm so that it's not recognizably quicksort? Not being facetious here, just wondering?
justinhj
quicksort on linked lists is stable. You take the first element as the pivot, and then partition by appending to the two parts. Relative order will be preserved in each sublist, so that kind of quicksort *is* stable.
Martin v. Löwis
but if we follow above partioning algo then i dont see there is any loss of stability ,so can you plz give a set of test data which can demonstrate that it is unstable.
mawia
+2  A: 

If you need a stable O(n*log(n)) sort, use mergesort. (The best way to make quicksort stable by the way is to chose a median of random values as the pivot. This is not stable for all elements equivalent, however.)

JP Alioto
If it's not stable when all elements are the same, then it's not stable.
Joe White
@Joe: That special case can be handled in O(n) time, so it does not affect the O(n*log(n)) running time.
JP Alioto
I assume you mean "all elements equivalent". If all elements are the same, there's only one permutation possible, which by definition is always sorted.
MSalters
@MSalters: Corrected. Thank you. :)
JP Alioto
+2  A: 

Any sort can be converted to a stable sort if you're willing to add a second key. The second key should be something that indicates the original order, such as a sequence number. In your comparison function, if the first keys are equal, use the second key.

Mark Ransom
but if we follow above partioning algo then i dont see there is any loss of stability ,so can you plz give a set of test data which can demonstrate that it is unstable..
mawia
A: 

A sort is stable when the original order of similar elements doesn't change. Your algorithm isn't stable since it swaps equal elements.

If it didn't, then it still wouldn't be stable:

( 1, 5, 2, 5, 3 )

You have two elements with the sort key "5". If you compare element #2 (5) and #5 (3) for some reason, then the 5 would be swapped with 3, thereby violating the contract of a stable sort. This means that carefully choosing the pivot element doesn't help, you must also make sure that the copying of elements between the partitions never swaps the original order.

Aaron Digulla