Quicksort Array in Java

Quicksort is a divide and conquer algorithm. It first divides a large list into two smaller sub-lists and then recursively sort the two sub-lists. If we want to sort an array without any extra space, quicksort is a good option. On average, time complexity is O(n log(n)).

The basic step of sorting an array are as follows:

  • Select a pivot, normally the middle one
  • From both ends, swap elements and make left elements < pivot and all right > pivot
  • Recursively sort left part and right part

Here is a very good animation of quicksort.

public class QuickSort {
	public static void main(String[] args) {
		int[] x = { 9, 2, 4, 7, 3, 7, 10 };
		int low = 0;
		int high = x.length - 1;
		quickSort(x, low, high);
	public static void quickSort(int[] arr, int low, int high) {
		if (arr == null || arr.length == 0)
		if (low >= high)
		// pick the pivot
		int middle = low + (high - low) / 2;
		int pivot = arr[middle];
		// make left < pivot and right > pivot
		int i = low, j = high;
		while (i <= j) {
			while (arr[i] < pivot) {
			while (arr[j] > pivot) {
			if (i <= j) {
				int temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
		// recursively sort two sub parts
		if (low < j)
			quickSort(arr, low, j);
		if (high > i)
			quickSort(arr, i, high);


9 2 4 7 3 7 10
2 3 4 7 7 9 10
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Jack

    Doesn’t one of the pivot conditionals need to have an equals, such as =, instead of ? Otherwise, what happens if the pivot value occurs multiple times in the array?

  • Zinga Zee

    try this

    public void quickSort(int[] arr, int p, int r){

    if(p < r) {

    int q = partition(arr, p, r);

    quickSort(arr, p, q-1);

    quickSort(arr, q+1, r);



    public int partition(int arr[], int p, int r)


    int i = p-1;

    int pivot = arr[r];

    for (int j = p; j <= r; j++) {

    if(arr[j] <= pivot){


    //do the swap


    arr[i] = arr[i] ^ arr[j];

    arr[j] = arr[i] ^ arr[j];

    arr[i] = arr[i] ^ arr[j];




    return i;


  • Nate Neu

    Just a real hint ( low + high ) >> 1 is even faster

  • Sergey Dinamik

    I’m thinking about it 30 mins already ) voting for “yes, we can”

  • kk

    I could be wrong but I think the running time for the partition part could be O(n^2) so this Quicksort does not have O(nlgn) running time.

  • Anirudh Mathad

    Can we remove the if conditions here :

    if (low i)

    quickSort(arr, i, high);

    Inside the function we are again checking the same.

  • Nanda firizki

    i like this program and i understand it , thanks bro

  • Dylan Yiyang Qiu

    In recursively sort two sub parts, it seems you don’t need to check relationship between low and j (high and i) before you call quickSort. Since you define a stop condition of low>=high at the beginning.

  • u2

    2(low)/2+(high-low)/2 = (2low-low+high)/2 = (low+high)/2.

  • Elver

    shouldn’t “if(i <= j)" just be "if(i < j)"? What is the point to swap i and j if both index where equal?

  • JavaPrograms

    check this program with explanation http://goo.gl/6d529h

  • theLastUnicorn

    ah no? He’s referring to get the difference first: (high – low), then use this divided by 2, then add to the starting position, which “low+” will be the last operation.

  • crackerplace

    I just wanted to understand one point.The above quickSort method excluding the recursion part modifies the array such that all elements less than pivot are on left side and all elements greater than pivot are on right side.Is this correct.My point is that the pivot will not be at the boundary but it will be somewhere in the right part ?

  • Pravesh Jain

    Just a little problem with your statement. You mean to say they will not retain their *order* after sorting. Not places.

  • Govind

    Good tutorial, but it’s important to note that it’s not stable. If you have same numbers they will not retain their places after sorting. By the way you can also use following quicksort algorithm to sort it in-place.

  • Ashish Thakran

    Quicksort is slightly sensitive to input that happens to be in the right order, in which case it can skip some swaps. Mergesort doesn’t have any such optimizations, which also makes Quicksort a bit faster compared to Mergesort.

    To know more about quicksort and mergesort, below link can be useful

    Why Quick sort is better than Merge sort

  • kd

    Just a little hint: (low + high) / 2 = low + (high – low) / 2.