Sorting an array of literals using quick sort in JavaScript

We are required to write a JavaScript function that takes in an array of numbers and uses the quick sort algorithm to sort it.

QuickSort Algorithm

QuickSort is a divide-and-conquer algorithm that works by selecting a 'pivot' element from the array and partitioning other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.

How QuickSort Works

The algorithm follows these steps:

  • Choose a pivot element from the array (typically the middle element)
  • Partition the array so elements smaller than pivot go to the left, larger elements go to the right
  • Recursively apply the same process to the sub-arrays
  • Combine the results to get the final sorted array

Implementation

const arr = [43, 3, 34, 34, 23, 232, 3434, 4, 23, 2, 54, 6, 54];

// QuickSort function with left and right pointers
const quickSort = (arr, left = 0, right = arr.length - 1) => {
    let len = arr.length, index;
    
    if(len > 1) {
        index = partition(arr, left, right);
        
        // Recursively sort left sub-array
        if(left < index - 1) {
            quickSort(arr, left, index - 1);
        }
        
        // Recursively sort right sub-array  
        if(index < right) {
            quickSort(arr, index, right);
        }
    }
    return arr;
};

// Partition function to arrange elements around pivot
const partition = (arr, left, right) => {
    let middle = Math.floor((right + left) / 2);
    let pivot = arr[middle];
    let i = left; // Left pointer
    let j = right; // Right pointer
    
    while(i <= j) {
        // Move left pointer until we find element >= pivot
        while(arr[i] < pivot) {
            i++;
        }
        
        // Move right pointer until we find element <= pivot
        while(arr[j] > pivot) {
            j--;
        }
        
        // Swap elements if pointers haven't crossed
        if(i <= j) {
            [arr[i], arr[j]] = [arr[j], arr[i]]; // ES6 destructuring swap
            i++;
            j--;
        }
    }
    return i;
};

console.log("Original array:", arr);
console.log("Sorted array:", quickSort(arr));

Output

Original array: [
  43, 3,    34,   34,  23,
  232, 3434, 4,    23,  2,
  54, 6,    54
]
Sorted array: [
  2,  3,  4,   6,   23,
  23, 34, 34,  43,  54,
  54, 232, 3434
]

Time Complexity

Case Time Complexity Description
Best Case O(n log n) Pivot divides array into equal halves
Average Case O(n log n) Random pivot selection
Worst Case O(n²) Pivot is always smallest/largest element

Key Features

  • In-place sorting: Requires only O(log n) additional memory
  • Unstable: Does not preserve relative order of equal elements
  • Efficient: Generally faster than other O(n log n) algorithms in practice

Conclusion

QuickSort is an efficient divide-and-conquer algorithm ideal for large datasets. While it has O(n²) worst-case complexity, its average O(n log n) performance and in-place nature make it widely used in practice.

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

456 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements