How to implement quick sort in JavaScript?

In this article, we are going to discuss how to implement quick sort in JavaScript with suitable examples.

Quick Sort

The Quick sort is a divide and conquer algorithm similar to the merge sort. In this, we pick a pivot element and divide the array around the pivot element. There are many ways to pick the pivot element.

  • Always pick the first element as a pivot element.

  • Always pick the last element as a pivot element. (Implemented in the below program)

  • Pick a random element as pivot element

  • Pick the middle element as pivot element.

The main process in quick sort is partition. The aim of partition is, given an array and consider an element x in the array as pivot element. Keep the pivot element at the correct position in sorted array. Then put all the elements which are smaller than the pivot element in left side and the elements which are greater than the pivot element will be on the right side.

Input and Output Scenarios

Consider an array having some elements in it in a random order which are not sorted, we can sort the elements by performing quick sort. Let's check the scenario below.

Input = [0, 10, 4, 1, 3];
Output = 0, 1, 3, 4, 10

How Quick Sort Works

To understand how quick sort works, let's trace through an example with array arr=[7, 4, 10, 6, 3, 9]:

  • Indexes are 0 1 2 3 4 5

  • Low = 0, High = 5 and pivot element = 9 (last element)

  • Initially the index of smaller element, i = -1

Initial Array with Pivot = 9 7 i=0 4 i=1 10 i=2 6 i=3 3 i=4 9 pivot Elements < 9 (blue) Elements > 9 (red) Pivot (yellow)

The partitioning process compares each element with the pivot and rearranges them so that smaller elements are on the left and larger elements are on the right.

Implementation

In the following example, we implement quick sort using the last element as the pivot:

<!DOCTYPE html>
<html>
<head>
   <title>Implementation of Quick Sort</title>
</head>
<body>
   <script>
      function quickSort(array) {
         // Base case: arrays with 0 or 1 element are already sorted
         if (array.length < 2) {
            return array;
         }
         
         // Choose last element as pivot
         let pivot = array[array.length - 1];
         let leftArray = [];
         let rightArray = [];
         
         // Partition: elements smaller than pivot go left, larger go right
         for (let i = 0; i < array.length - 1; i++) {
            if (array[i] < pivot) {
               leftArray.push(array[i]);
            } else {
               rightArray.push(array[i]);
            }
         }
         
         // Recursively sort left and right subarrays, then combine
         return [...quickSort(leftArray), pivot, ...quickSort(rightArray)];
      }
      
      const originalArray = [0, 10, 4, 1, 3];
      const sortedArray = quickSort(originalArray);
      
      document.write("Original Array: " + originalArray + "<br>");
      document.write("Sorted Array: " + sortedArray);
   </script>
</body>
</html>
Original Array: 0,10,4,1,3
Sorted Array: 0,1,3,4,10

Time Complexity

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

Key Points

  • Quick sort is an in-place sorting algorithm with good average performance

  • Choice of pivot affects performance significantly

  • The algorithm uses divide and conquer approach

  • Space complexity is O(log n) due to recursive calls

Conclusion

Quick sort is an efficient sorting algorithm with O(n log n) average time complexity. The key is choosing a good pivot element and partitioning the array correctly around it.

Updated on: 2026-03-15T23:18:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements