Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quicksort - conditions that makes it stable

A sorting algorithm is stable if it preserves the relative order of any two elements with equals keys. Under which conditions is quicksort stable?

Quicksort is stable when no item is passed unless it has a smaller key.

What other conditions make it stable?

like image 237
Paradox Avatar asked Apr 27 '11 12:04

Paradox


People also ask

How can Quicksort be stable?

Yes, naive quicksort is stable because the partitioning algorithm is stable since it maintains the relative ordering of equal items.

Is Quicksort stable and inplace?

Quicksort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined. Efficient implementations of Quicksort are not a stable sort, meaning that the relative order of equal sort items is not preserved.

What are the major properties of Quicksort?

Quick Sort is a divide and conquer algorithm. It creates two empty arrays to hold elements less than the pivot value and elements greater than the pivot value, and then recursively sort the sub arrays. There are two basic operations in the algorithm, swapping items in place and partitioning a section of the array.

Is 3 way Quicksort stable?

No, 3-way quick sort is also not stable. For instance, if arr[i]>pivot and arr[j]<pivot , then these values will be swapped, and the next comparison will be with arr[i+1] and arr[j-1] .


2 Answers

Well, it is quite easy to make a stable quicksort that uses O(N) space rather than the O(log N) that an in-place, unstable implementation uses. Of course, a quicksort that uses O(N) space doesn't have to be stable, but it can be made to be so.

I've read that it is possible to make an in-place quicksort that uses O(log N) memory, but it ends up being significantly slower (and the details of the implementation are kind of beastly).

Of course, you can always just go through the array being sorted and add an extra key that is its place in the original array. Then the quicksort will be stable and you just go through and remove the extra key at the end.

like image 95
Justin Peel Avatar answered Sep 28 '22 03:09

Justin Peel


The condition is easy to figure out, just keep the raw order for equal elements. There is no other condition that has essential differences from this one. The point is how to achieve it.

There is a good example.

1. Use the middle element as the pivot.

2. Create two lists, one for smaller, the other for larger.

3. Iterate from the first to the last and put elements into the two lists. Append the element smaller than or equals to and ahead of the pivot to the smaller list, the element larger than or equals to and behind of the pivot to the larger list. Go ahead and recursive the smaller list and the larger list.

https://www.geeksforgeeks.org/stable-quicksort/

The latter 2 points are both necessary. The middle element as the pivot is optional. If you choose the last element as pivot, just append all equal elements to the smaller list one by one from the beginning and they will keep the raw order in nature.

like image 29
W.Perrin Avatar answered Sep 28 '22 02:09

W.Perrin