Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

Quick Sort

What is Quick Sort?

Quick sort is a highly efficient Divide and Conquer sorting algorithm. It selects a pivot element and partitions the array so all elements less than the pivot come before it and all greater elements come after. It then recursively sorts both partitions.

Quick sort is the fastest sorting algorithm in practice for most inputs — it has excellent cache performance and low overhead. It is the basis of most standard library sort implementations.

CaseTimeSpaceStable?
Best / AverageO(n log n)O(log n)No
WorstO(n²) — sorted array with last-element pivotO(n)No
Quick Sort — Lomuto and Hoare Partition
import java.util.Arrays;

public class QuickSort {

    // Lomuto partition scheme — pivot = last element
    static int partitionLomuto(int[] arr, int low, int high) {
        int pivot = arr[high];  // choose last element as pivot
        int i = low - 1;        // index of smaller element

        for (int j = low; j < high; j++) {
            if (arr[j] <= pivot) {
                i++;
                int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
            }
        }
        // Place pivot in correct position
        int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;
        return i + 1;  // pivot index
    }

    static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int pi = partitionLomuto(arr, low, high);
            quickSort(arr, low, pi - 1);   // sort left of pivot
            quickSort(arr, pi + 1, high);  // sort right of pivot
        }
    }

    // Hoare partition scheme — more efficient (fewer swaps)
    static int partitionHoare(int[] arr, int low, int high) {
        int pivot = arr[low];
        int i = low - 1, j = high + 1;
        while (true) {
            do { i++; } while (arr[i] < pivot);
            do { j--; } while (arr[j] > pivot);
            if (i >= j) return j;
            int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {10, 7, 8, 9, 1, 5};
        System.out.println("Before: " + Arrays.toString(arr));
        quickSort(arr, 0, arr.length - 1);
        System.out.println("After:  " + Arrays.toString(arr));
        // After: [1, 5, 7, 8, 9, 10]
    }
}

/*
 * Partition trace for [10, 7, 8, 9, 1, 5], pivot=5:
 * i=-1, j=0: arr[0]=10 > 5, skip
 * j=1: arr[1]=7 > 5, skip
 * j=2: arr[2]=8 > 5, skip
 * j=3: arr[3]=9 > 5, skip
 * j=4: arr[4]=1 <= 5, i=0, swap arr[0] and arr[4] → [1,7,8,9,10,5]
 * Place pivot: swap arr[1] and arr[5] → [1,5,8,9,10,7]
 * pivot index = 1
 */
import java.util.Arrays;
import java.util.Random;

public class RandomizedQuickSort {

    static Random rand = new Random();

    // Randomized pivot — avoids O(n²) worst case on sorted input
    static int randomPartition(int[] arr, int low, int high) {
        int randomIdx = low + rand.nextInt(high - low + 1);
        // Swap random element with last element
        int temp = arr[randomIdx]; arr[randomIdx] = arr[high]; arr[high] = temp;

        // Now use standard Lomuto partition
        int pivot = arr[high];
        int i = low - 1;
        for (int j = low; j < high; j++) {
            if (arr[j] <= pivot) {
                i++;
                temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
            }
        }
        temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;
        return i + 1;
    }

    static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int pi = randomPartition(arr, low, high);
            quickSort(arr, low, pi - 1);
            quickSort(arr, pi + 1, high);
        }
    }

    public static void main(String[] args) {
        int[] arr = {3, 6, 8, 10, 1, 2, 1};
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
        // [1, 1, 2, 3, 6, 8, 10]
    }
}

/*
 * Randomized Quick Sort:
 * - Expected O(n log n) for ALL inputs
 * - Eliminates worst case for sorted/reverse-sorted arrays
 * - Used in practice (Java's Arrays.sort for primitives uses dual-pivot quicksort)
 */

Quick Sort — Pivot Selection Strategies

StrategyPivot ChoiceWorst Case InputNotes
Last elementarr[high]Already sorted arraySimple, common in textbooks
First elementarr[low]Already sorted arraySame issue as last element
Median of threeMedian of first, mid, lastRare in practiceBetter average performance
Random pivotRandom elementNo deterministic worst caseExpected O(n log n) always

Quick Sort vs Merge Sort

FeatureQuick SortMerge Sort
Average timeO(n log n)O(n log n)
Worst caseO(n²)O(n log n)
SpaceO(log n) — in-placeO(n) — extra array
Stable?NoYes
Cache performanceExcellent (sequential access)Good
Practical speedFaster in practiceSlower due to memory allocation
Used inJava Arrays.sort (primitives), C++ std::sortJava Arrays.sort (objects), Python sort

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.