Data Structure and Algorithm
Algorithms
Sorting
Quick Sort

Quick Sort in JavaScript - Complete Guide

Introduction

Quicksort is a Divide and Conquer Algorithm that is used for sorting elements. In this algorithm, we choose a pivot and partition the given array according to the pivot. Quicksort is a highly used algorithm because it is cache-friendly and performs in-place sorting of the elements, meaning no extra space is inherently required for manipulating the input.

Note: The default implementation of Quicksort is an unstable algorithm because it cannot maintain the relative order of equal elements. However, any sorting algorithm can be made stable by considering indexes as a comparison parameter.

Three Partitions of Quicksort

Three partition schemes are commonly used for the Quicksort algorithm:

  1. Naive partition: This partition helps to maintain the relative order of the elements but it takes O(n) extra space.
  2. Lomuto partition: In this partition, the last element is chosen as a pivot. The pivot acquires its required position after partitioning, but more comparisons take place.
  3. Hoare's partition: In this partition, the first element is chosen as a pivot. The pivot displaces to its required position after partitioning, and less comparisons take place compared to the Lomuto partition.

1. Naive Partition

Algorithm

Naivepartition(arr[], l, r)

1. Make a Temporary array temp[r-l+1] length
2. Choose last element as a pivot element
3. Run two loops:
   -> Store all the elements in the temp array that are less than pivot element
   -> Store the pivot element
   -> Store all the elements in the temp array that are greater than pivot element.
4. Update all the elements of arr[] with the temp[] array

Implementation

function partitionNaive(arr, l, h, p) {
    let temp = []; 
    let index = 0;
    
    // Store all elements less than or equal to pivot
    for (let i = l; i <= h; i++) {
        if (arr[i] <= arr[p] && i !== p) {
            temp[index] = arr[i];
            index++;
        }
    }
    
    // Store the pivot
    temp[index++] = arr[p];
    
    // Store all elements greater than pivot
    for (let i = l; i <= h; i++) {
        if (arr[i] > arr[p]) {
            temp[index] = arr[i];
            index++;
        }
    }
    
    // Update original array
    for (let i = l; i <= h; i++) {
        arr[i] = temp[i - l];
    }
    
    return arr;
}

2. Quick Sort using Lomuto Partition

Lomuto's Partition Scheme

This algorithm works by assuming the pivot element as the last element. If any other element is given as a pivot element, then swap it first with the last element. Now initialize two variables i and j as low, iterate over the array and increment i when arr[j] <= pivot and swap arr[i] with arr[j], otherwise increment only j. After coming out from the loop swap arr[i+1] with arr[hi]. This i+1 stores the pivot element.

Algorithm

partition(arr[], lo, hi)
  pivot = arr[hi]
  i = lo - 1     // place for swapping
  for j := lo to hi - 1 do
      if arr[j] <= pivot then
          i = i + 1
          swap arr[i] with arr[j]
  swap arr[i+1] with arr[hi]
  return i + 1

Implementation

function lPartition(arr, l, h) {
    let pivot = arr[h];
    let i = l - 1;
    
    for (let j = l; j <= h - 1; j++) {
        if (arr[j] < pivot) {
            i++;
            [arr[i], arr[j]] = [arr[j], arr[i]];
        }
    }
    
    [arr[i + 1], arr[h]] = [arr[h], arr[i + 1]];
    return i + 1; 
}
 
function qSortLomuto(arr, l, h) {
    if (l < h) {
        let p = lPartition(arr, l, h);
        qSortLomuto(arr, l, p - 1);
        qSortLomuto(arr, p + 1, h);
    }
    return arr;
}
 
let arrLomuto = [10, 80, 30, 90, 40, 50, 70];
console.log(qSortLomuto(arrLomuto, 0, arrLomuto.length - 1));
// Output: [10, 30, 40, 50, 70, 80, 90]

3. Quick Sort using Hoare Partition

Hoare's Partition Scheme

Hoare's Partition Scheme works by initializing two indexes that start at two ends. The two indexes move toward each other until an inversion is found (A smaller value on the left side and a greater value on the right side). When an inversion is found, two values are swapped, and the process is repeated.

Algorithm

partition(arr[], lo, hi)
  pivot = arr[lo]
  i = lo - 1  // Initialize left index
  j = hi + 1  // Initialize right index

  // Find a value in left side greater than pivot
  do
      i = i + 1
  while arr[i] < pivot

  // Find a value in right side smaller than pivot
  do
      j = j - 1
  while arr[j] > pivot

  if i >= j then
      return j

  swap arr[i] with arr[j]

Implementation

function hPartition(arr, l, h) {
    let pivot = arr[l];
    let i = l - 1, j = h + 1;
    
    while (true) {
        do {
            i++;
        } while (arr[i] < pivot);
        
        do {
            j--;
        } while (arr[j] > pivot);
        
        if (i >= j) return j;
        
        [arr[i], arr[j]] = [arr[j], arr[i]];
    }
}
 
function qSortHoare(arr, l, h) {
    if (l < h) {
        let p = hPartition(arr, l, h);
        qSortHoare(arr, l, p);
        qSortHoare(arr, p + 1, h);
    }
    return arr;
}
 
let arrHoare = [5, 3, 8, 4, 2, 7, 1, 10];
console.log(qSortHoare(arrHoare, 0, arrHoare.length - 1));

Note: If we change Hoare's partition to pick the last element as pivot, then Hoare's partition may cause QuickSort to go into an infinite recursion. For example, (10, 5, 6, 20) and pivot is arr[high], then returned index will always be high and call to same QuickSort will be made. To handle a random pivot, we can always swap that random element with the first element and simply follow the above algorithm.

Comparison: Hoare vs Lomuto

  1. Efficiency: Hoare's scheme is more efficient than Lomuto's partition scheme because it does three times fewer swaps on average, and it creates efficient partitions even when all values are equal.
  2. Degradation: Like Lomuto's partition scheme, Hoare partitioning also causes Quick sort to degrade to O(n²) when the input array is already sorted.
  3. Pivot Location: Note that in Hoare's scheme, the pivot's final location is not necessarily at the index that was returned, and the next two segments that the main algorithm recurses on are (lo..p) and (p+1..hi) as opposed to (lo..p-1) and (p+1..hi) as in Lomuto's scheme.
  4. Stability: Both Hoare's Partition, as well as Lomuto's partition, are unstable.
FeatureHoare Partition AlgorithmLomuto Partition Algorithm
Pivot SelectionGenerally, the first item is assumed to be the initial pivot element.Generally, the last element of the list is considered as the initial pivot element.
ComplexityIt is a linear algorithm.It is also a linear algorithm.
SpeedIt is relatively faster.It is slower.
ImplementationIt is slightly difficult to understand and implement.It is easy to understand and implement.
Pivot PlacementIt doesn't fix the pivot element in the correct position.It fixes the pivot element in the correct position.

QuickSort Analysis

Is QuickSort in-place?

As per the broad definition of an in-place algorithm, it qualifies as an in-place sorting algorithm as it uses extra space only for storing recursive function calls but not for manipulating the input.

What is 3-Way QuickSort?

In a simple Quicksort algorithm, we select an element as pivot, partition the array around pivot and recur for subarrays on left and right of pivot. Consider an array which has many redundant elements. For example, {1, 4, 2, 4, 2, 4, 1, 2, 4, 1, 2, 2, 2, 2, 4, 1, 4, 4, 4}. If 4 is picked as pivot in Simple QuickSort, we fix only one 4 and recursively process remaining occurrences. In a 3-Way QuickSort, an array arr[l..r] is divided in 3 parts:

  • arr[l..i] elements less than pivot
  • arr[i+1..j-1] elements equal to pivot
  • arr[j..r] elements greater than pivot

Why Quick Sort is preferred over MergeSort for sorting Arrays?

Quick Sort in its general form is an in-place sort (i.e., it doesn't require any extra storage) whereas merge sort requires O(N) extra storage. Allocating and de-allocating the extra space used for merge sort increases the running time of the algorithm. Comparing average complexity we find that both type of sorts have O(N log N) average complexity but the constants differ. Quick Sort is also a cache friendly sorting algorithm as it has good locality of reference when used for arrays.

Why MergeSort is preferred over QuickSort for Linked Lists?

In the case of linked lists, the case is different mainly due to difference in memory allocation of arrays and linked lists. Unlike arrays, linked list nodes may not be adjacent in memory. In arrays, we can do random access as elements are continuous in memory. Quick Sort requires a lot of this kind of access. In a linked list, to access the i'th index, we have to travel each and every node from the head to the i'th node as we don't have a continuous block of memory. Therefore, the overhead increases for quicksort. Merge sort accesses data sequentially and the need of random access is low.

Advantages of Quick Sort:

  • It is a divide-and-conquer algorithm that makes it easier to solve problems.
  • It is highly efficient on large data sets.
  • It has a low overhead, as it only requires a small amount of memory to function.
  • It is cache friendly.

Disadvantages of Quick Sort:

  • It has a worst-case time complexity of O(n²), which occurs when the pivot is chosen poorly (e.g., already sorted array with naive pivot selection).
  • It is not a good choice for small data sets.
  • It can be sensitive to the choice of pivot.

Time Complexity

Time taken by QuickSort in general can be written as following: T(n) = T(k) + T(n-k-1) + θ(n) The first two terms are for two recursive calls, the last term is for the partition process. k is the number of elements which are smaller than pivot. The time taken by QuickSort depends upon the input array and partition strategy.

Worst Case: The worst case occurs when the partition process always picks greatest or smallest element as pivot. If we consider above partition strategy where last element is always picked as pivot, the worst case would occur when the array is already sorted in increasing or decreasing order.

T(n) = T(0) + T(n-1) + θ(n)
which is equivalent to  
T(n) = T(n-1) + θ(n)

The solution of above recurrence is O(n²).

Best Case: The best case occurs when the partition process always picks the middle element as pivot.

T(n) = 2T(n/2) + θ(n)

The solution of above recurrence is θ(n log n). It can be solved using case 2 of Master Theorem.

Average Case: To do average case analysis, we need to consider all possible permutations of the array and calculate time taken by every permutation. We can get an idea of average case by considering the case when partition puts O(n/9) elements in one set and O(9n/10) elements in other set.

T(n) = T(n/9) + T(9n/10) + θ(n)

Solution of above recurrence is also O(n log n).

Summary

  • Quicksort is a fast and efficient sorting algorithm with an average time complexity of O(n log n).
  • It is a divide-and-conquer algorithm that breaks down the original problem into smaller subproblems that are easier to solve.
  • It can be easily implemented in both iterative and recursive forms and it is efficient on large data sets, and can be used to sort data in-place.
  • However, it also has some drawbacks such as worst case time complexity of O(n²) which occurs when the pivot is chosen poorly.
  • It is not a good choice for small datasets, and is sensitive to the choice of pivot.