logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
ArraysBoyer-Moore Majority Vote AlgorithmKadane’s AlgorithmTriple Reverse algorithm aka Reversal Algorithm
Cycle detectionDijkstraDisjoint Set UnionGraphTopological SortTraversal algorithmsTree Traversal
Binary HeapBinomial HeapHeap
Cycle DetectionLinked ListSorting
Finding the majority elementRotate array by K steps
Finding If Number is PrimeGCD of two numbersPerforming ExponentiationPrime NumbersRandomising Data
3. Max Consecutive OnesHoare'S Partition SchemeLomuto Partition Scheme
DequesPriority QueueQueue
The Binary Search Master GuideThe Rotated Sorted Array Master Guide
Bubble SortCounting SortInsertion SortMerge SortPigeon Hole SortQuick SortSelection Sort
Monotonic StackMonotonic StackStackReversing an ArrayStackTower of Hanoi
Binary Tree TraversalCentre of gravityDiameter of TreeShortest Path
Algorithmic Pattern/sort

Quick Sort

3 min read
On this pageImplementation
ImplementationDivide PartConquer Part Combine PartReferences
  • based on the idea of divide-and-conquer
  • is cache-friendly due to its in-place sorting property and reduced memory accesses.

# Intuition of quick sort

  • partition an unsorted array around some input value (pivot) into two parts such that values in the left part are less than the pivot and values in the right part are greater than the pivot.

  • the pivot will get placed in the correct position in the sorted output after the partition

  • if we sort both halves recursively, using the same function, the entire array will get sorted

    • repeat the same process for smaller subarrays until we reach the base case.

# Implementation

# Divide Part

  • Divide the problem into two smaller subproblems by partitioning the array X[l...r]X[l...r]X[l...r].
  • The partition process will return the pivotIndex i.e. the index of the pivot after the partition.
    • Subproblem 1: Sort array X[] from l to pivotIndex - 1.
    • Subproblem 2: Sort array X[] from pivotIndex + 1 to r.
  • Define the partition(X,l,r)partition(X, l, r)partition(X,l,r) function to divide the array around the pivot and return the pivotIndexpivotIndexpivotIndex.

# Partition algorithm

  • Choose the rightmost value as the pivot, i.e., pivot=X[r]pivot = X[r]pivot=X[r], and scan the array from left to right.
  • The starting part of the array should contain values less than the pivot.
    • Whenever an element is less than the pivot, we will place it at the starting part of the array and move forward.
    • Otherwise, ignore that element and move forward.
int partition (int X[], int l, int r)
{
    int pivot = X[r]  
    int i = l - 1
    for (int j = l; j < r; j = j + 1)
    {
        if (X[j] < pivot)
        {
            i = i + 1
            swap (X[i], X[j])
        }
    }
    swap (X[i + 1], X[r])
    return i + 1
}

# Conquer Part

  • Sort both subarrays recursively.
    • recursively sort the left subarray by calling the same function i.e., quickSort(X,l,pivotIndex−1)quickSort(X, l, pivotIndex - 1)quickSort(X,l,pivotIndex−1)
    • recursively sort the right subarray by calling the same function i.e., quickSort(X,pivotIndex+1,r)quickSort(X, pivotIndex + 1, r)quickSort(X,pivotIndex+1,r)

Base case

  • The base case occurs when the sub-array size is either 0 (empty) or 1.
    • In other words, l >= r is the condition for the base case.
void quickSort(int X[], int l, int r)
{
    if (l < r)
    {
        int pivotIndex = partition(X, l, r)
        quickSort(X, l, pivotIndex - 1)
        quickSort(X, pivotIndex + 1, r)
    }
}

# Combine Part

  • A trivial case after sorting both smaller arrays, the entire array will be sorted.
    • There is no need for the combine part.\

# References

  • Quick Sort Algorithm

    \

←PreviousPigeon Hole SortNext→Selection Sort

On this page

ImplementationDivide PartConquer Part&#x20;Combine PartReferences