Dual Pivot Quick Sort: Java’s default sorting algorithm for primitive data types.
Tags: sorting, java, programming, algorithms
Published: 2018-07-29 07:00:09

---
title: Dual Pivot Quick Sort: Java’s default sorting algorithm for primitive data types.
published: true 
description: Java default sorting algorithm.
tags: sorting, java, programming, algorithm 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/djkftko0ww0ddrjux8hn.jpg
---

What happens when we do Arrays.sort() in Java? which sorting algorithm does Java use in the background? 

Since Java 7 release back in 2011, default sorting algorithm used is DualPivotQuickSort which is an enhancement over classic quicksort algorithm. 

Dual pivot quicksort is a combination of insertion sort and quick sort. Insertion sort has faster runtime when the number of elements to be sorted is small,  Double pivot quicksort uses this fact thus when the number of elements is <= 47 Java performs insertion sort under the hood. 

When input size array is larger than 47 Java uses Double pivot quicksort. As the name suggests DualPivotQuickSort algorithm picks 2 pivots instead of 1. Unlike quicksort in which we partition the array around 1 pivot, we partition the array in three parts around 2 pivots. 

1st sub array  :  items < LP
2nd sub array  :  LP <= items <= RP
3rd sub array  =:  items >= RP

**Algorithm**

LP: Left Pivot 
RP: Right Pivot

1.) Left most item in an array is taken as the left pivot and Rightmost item as the right pivot. 
2.) Swap Left pivot with Right Pivot if LP > RP
3.) Partition array in three sub-arrays around left and right pivot.

Once a partition is done we perform above 3 steps recursively on three sub-array.

Let’s walk through an example-:

![Alt text of image](https://thepracticaldev.s3.amazonaws.com/i/n6mp5fdei3q441ycdn4y.jpg)

No need to swap pivots in above image since LP < RP. 
Now we partition the array following below schemes.

1st sub array  :  items < LP
2nd sub array  :  LP <= items <= RP
3rd sub array  =:  items >= RP

![Alt text of image](https://thepracticaldev.s3.amazonaws.com/i/a3eq0j679g59dthcw42d.jpg)

Now we have 3 sub-array over which we'll perform the same steps as above. Since the first two arrays have only 2 items, one will become left pivot and other will become right pivot. We'll swap left pivot with right pivot if the left pivot is greater than right pivot which is not the case for first two sub-arrays.

![Alt text of image](https://thepracticaldev.s3.amazonaws.com/i/ehghis7p9tsd9g1wm4q4.jpg)

For the third sub-array as we can see in below image left pivot is greater than right pivot thus we'll swap them.

![Alt text of image](https://thepracticaldev.s3.amazonaws.com/i/g95l8m8pa9g66l0v3ov0.jpg)

Swapping pivots.
![Alt text of image](https://thepracticaldev.s3.amazonaws.com/i/6f473lb9nolxytzohalq.jpg)

We don't have any more elements in the array that we can partition further.
![Alt text of image]
(https://thepracticaldev.s3.amazonaws.com/i/13zl7hgjyjuz0tkpilhr.jpg)

![Alt text of image]
(https://thepracticaldev.s3.amazonaws.com/i/nwl0r1opeozphmvzzwby.jpg)

**References**

https://www.geeksforgeeks.org/dual-pivot-quicksort/
https://stackoverflow.com/questions/20917617/whats-the-difference-of-dual-pivot-quick-sort-and-quick-sort
http://codeblab.com/wp-content/uploads/2009/09/DualPivotQuicksort.pdf

**Conclusion**
Similarly, Python uses Timsort which is a combination of insertion sort and merge sort. I’d like to read and write about different other variation of quicksort in the next few days. 

---

If you want me to write on a specific topic, please feel free to post them in the comment section below. 

You can support my work by buying me a coffee below. 💚💚💚💚💚💚 !!
[![Buy me a ko-fi](https://thepracticaldev.s3.amazonaws.com/i/5xarm26ij4p1hwuqh9vr.png)](https://ko-fi.com/F1F0J8U1)