JavaScript Quicksort Recursive

In this article, we will learn to implement Quicksort recursively in JavaScript. Quicksort is one of the most efficient and widely used sorting algorithms, known for its divide-and-conquer approach.

What is Quicksort?

Quicksort is a divide-and-conquer sorting algorithm that sorts an array by selecting a pivot element and partitioning the array into two subarrays: elements smaller than the pivot and elements greater than or equal to the pivot. It then recursively sorts the subarrays.

Quicksort Process Original Array: 5 3 7 6 2 9 ? Pivot (7) After Partition: Left (< 7): 5 3 6 2 7 Right (? 7): 9 Recursively sort Recursively sort

Algorithm

Quicksort follows these steps:

  • Step 1: Choose a pivot element (commonly first, last, or middle element)
  • Step 2: Partition the array so elements smaller than pivot are on left, larger on right
  • Step 3: Recursively apply quicksort to the left subarray
  • Step 4: Recursively apply quicksort to the right subarray

Pseudocode

QUICKSORT(arr, low, high):
    if low < high:
        pivotIndex = PARTITION(arr, low, high)
        QUICKSORT(arr, low, pivotIndex - 1)  // Left subarray
        QUICKSORT(arr, pivotIndex + 1, high)  // Right subarray

Implementation

Here's a complete recursive quicksort implementation in JavaScript:

const arr = [5, 3, 7, 6, 2, 9];

const swap = (arr, leftIndex, rightIndex) => {
    let temp = arr[leftIndex];
    arr[leftIndex] = arr[rightIndex];
    arr[rightIndex] = temp;
};

const partition = (arr, left, right) => {
    let pivot = arr[Math.floor((right + left) / 2)];
    let i = left;
    let j = right;
    
    while (i <= j) {
        while (arr[i] < pivot) {
            i++;
        }
        while (arr[j] > pivot) {
            j--;
        }
        if (i <= j) {
            swap(arr, i, j);
            i++;
            j--;
        }
    }
    return i;
};

const quickSort = (arr, left = 0, right = arr.length - 1) => {
    let index;
    if (arr.length > 1) {
        index = partition(arr, left, right);
        if (left < index - 1) {
            quickSort(arr, left, index - 1);
        }
        if (index < right) {
            quickSort(arr, index, right);
        }
    }
    return arr;
};

let sortedArray = quickSort(arr);
console.log(sortedArray);
[ 2, 3, 5, 6, 7, 9 ]

How It Works

The algorithm works by:

  • Partitioning: The partition() function rearranges elements around the pivot
  • Recursion: quickSort() calls itself on left and right subarrays
  • Base Case: Recursion stops when subarray has 1 or 0 elements

Complexity Analysis

Time Complexity:

Case Time Complexity Description
Best Case O(n log n) Pivot divides array into equal halves
Average Case O(n log n) Pivot creates reasonably balanced partitions
Worst Case O(n²) Pivot is always smallest or largest element

Space Complexity:

  • Best/Average Case: O(log n) due to recursion stack
  • Worst Case: O(n) when partitions are highly unbalanced

Conclusion

Quicksort is an efficient sorting algorithm with average O(n log n) performance. Its recursive implementation is intuitive and demonstrates the power of divide-and-conquer algorithms in JavaScript.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements