📊

Understanding Quick Sort Algorithm

Mar 5, 2025

Quick Sort - Lecture Notes

Introduction to Quick Sort

  • QuickSort is a sorting algorithm based on the Divide and Conquer principle.
  • It picks an element as a pivot and partitions the given array around this pivot.

How QuickSort Algorithm Works

  1. Choose a Pivot:
    • Select an element from the array as the pivot.
    • The choice of pivot can vary: first element, last element, random element, or median.
  2. Partition the Array:
    • Rearrange the array around the pivot.
    • After partitioning, all elements smaller than the pivot are on its left, and all elements greater are on its right.
    • The pivot is placed in its correct position.
  3. Recursively Call:
    • Apply the same process to the two partitioned sub-arrays (left and right of the pivot).
  4. Base Case:
    • Recursion stops when there is only one element left in the sub-array.

Choice of Pivot

  • First or Last Element as Pivot:
    • Simple but can lead to worst-case scenarios if the array is already sorted.
  • Random Element as Pivot:
    • Preferred as it avoids worst-case patterns.
  • Median Element as Pivot:
    • Ideal for time complexity but has high constants for median finding.

Partition Algorithm

  • Key process in QuickSort is partition(), with O(n) time complexity.
  • Naive Partition:
    • Creates a copy of the array, placing smaller elements first, followed by larger ones.
  • Lomuto Partition:
    • Simpler, used in most examples due to its simplicity.
  • Hoare’s Partition:
    • Faster, traverses from both ends and swaps elements.

Illustration of QuickSort Algorithm

  • Recursively call for smaller sub-arrays around the pivot until sorted.
  • Once elements are in their correct positions, the array is sorted.

QuickSort Implementations

  • Code examples in C++, Java, Python, C#, JavaScript, and PHP provided.
  • Key Functions:
    • Partition Function: Chooses the pivot and re-arranges elements.
    • Swap Function: Used to swap elements during partition.
    • QuickSort Function: Recursively sorts the array.

Complexity Analysis

  • Best Case: O(n log n)
  • Average Case: O(n log n)
  • Worst Case: O(n^2) when the pivot is poorly chosen.
  • Auxiliary Space: O(n) due to recursive call stack.

Advantages of Quick Sort

  • Efficient on large datasets.
  • Low overhead, small memory requirements.
  • Cache friendly.
  • Fastest general-purpose sorting algorithm.

Disadvantages of Quick Sort

  • Worst-case time complexity can be O(n^2).
  • Not stable (does not preserve the relative order of equal elements).
  • Not ideal for small datasets.

Applications of Quick Sort

  • Sorting large datasets efficiently.
  • Used in partitioning problems.
  • Beneficial in randomized algorithms.
  • Integral to cryptography for random permutations.
  • Parallelizable for multi-core systems.

Additional Resources

These notes provide a comprehensive overview of QuickSort, its workings, applications, and implementations in various programming languages.