# Stable QuickSort

A sorting algorithm is said to be stable if it maintains the relative order of records in the case of equality of keys.

Input : (1, 5), (3, 2) (1, 2) (5, 4) (6, 4)

We need to sort key-value pairs in the increasing order of keys of first digit

There are two possible solution for the two pairs where the key is same i.e. (1, 5) and (1, 2) as shown below:

OUTPUT1: (1, 5), (1, 2), (3, 2), (5, 4), (6, 4)

OUTPUT2: (1, 2), (1, 5), (3, 2), (5, 4), (6, 4)

A stable algorithm produces first output. You can see that (1, 5) comes before (1, 2) in the sorted order, which was the original order i.e. in the given input, (1, 5) comes before (1, 2).Second output can only be produced by an unstable algorithm. You can see that in the second output, the (1, 2) comes before (1, 5) which was not the case in the original input.

Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc.

QuickSort is an unstable algorithm because we do swapping of elements according to pivot’s position (without considering their original positions).

**How to make QuickSort stable?**

Quicksort can be stable but it typically isn’t implemented that way. Making it stable either requires order N storage (as in a naive implementation) or a bit of extra logic for an in-place version.

In below implementation, we use extra space. The idea is to make two separate lists:

1) First list contains items smaller than pivot.

2) Second list contains items greater than pivot.

`# Python code to implement Stable QuickSort. ` `# The code uses middle element as pivot. ` `def` `quickSort(ar): ` ` ` ` ` `# Base case ` ` ` `if` `len` `(ar) <` `=` `1` `: ` ` ` `return` `ar ` ` ` ` ` `# Let us choose middle element a pivot ` ` ` `else` `: ` ` ` `mid ` `=` `len` `(ar)` `/` `/` `2` ` ` `pivot ` `=` `ar[mid] ` ` ` ` ` `# key element is used to break the array ` ` ` `# into 2 halves according to their values ` ` ` `smaller,greater ` `=` `[],[] ` ` ` ` ` `# Put greater elements in greater list, ` ` ` `# smaller elements in smaller list. Also, ` ` ` `# compare positions to decide where to put. ` ` ` `for` `indx, val ` `in` `enumerate` `(ar): ` ` ` `if` `indx !` `=` `mid: ` ` ` `if` `val < pivot: ` ` ` `smaller.append(val) ` ` ` `elif` `val > pivot: ` ` ` `greater.append(val) ` ` ` ` ` `# If value is same, then considering ` ` ` `# position to decide the list. ` ` ` `else` `: ` ` ` `if` `indx < mid: ` ` ` `smaller.append(val) ` ` ` `else` `: ` ` ` `greater.append(val) ` ` ` `return` `quickSort(smaller)` `+` `[pivot]` `+` `quickSort(greater) ` ` ` `# Driver code to test above ` `ar ` `=` `[` `10` `, ` `7` `, ` `8` `, ` `9` `, ` `1` `, ` `5` `] ` `sortedAr ` `=` `quickSort(ar) ` `print` `(sortedAr) ` |

*chevron_right*

*filter_none*

**Output:**

[1, 5, 7, 8, 9, 10]

In above code, we have intentionally used middle element as pivot to demonstrate how to consider position as part of comparison. Code simplifies a lot if we use last element as pivot. In case of last element, we can always push equal elements in the smaller list.

## Recommended Posts:

- Stable Selection Sort
- Stable sort for descending order
- Position of an element after stable sort
- QuickSort
- Why quicksort is better than mergesort ?
- C++ Program for QuickSort
- Java Program for QuickSort
- QuickSort using Random Pivoting
- Dual pivot Quicksort
- Python Program for QuickSort
- When does the worst case of Quicksort occur?
- QuickSort on Singly Linked List
- QuickSort on Doubly Linked List
- 3-Way QuickSort (Dutch National Flag)
- Generic Implementation of QuickSort Algorithm in C

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.