Quick Sort

Quick Sort

See this video to understand the algorithm / how it is working : https://youtu.be/Vtckgz38QHs?si=m1UjUgqjp-osTY6p

Quick Sort is a divide-and-conquer sorting algorithm that works by selecting a "pivot" element and partitioning the array into two halves: one with elements smaller than the pivot and one with elements greater than the pivot. It then recursively sorts the two halves.


Algorithm for Quick Sort

  1. Start

  2. Define a function QuickSort(arr, low, high):

    • Step 2.1: If low < high:

      • Call the Partition function to find the pivotIndex.

      • Recursively call QuickSort for the left subarray (low to pivotIndex - 1).

      • Recursively call QuickSort for the right subarray (pivotIndex + 1 to high).

    • Otherwise, return (base case).

  3. Define the Partition(arr, low, high) function:

    • Step 3.1: Choose the pivot element as arr[high].

    • Step 3.2: Initialize an index i to low - 1.

    • Step 3.3: Loop through the array from low to high - 1 using index j:

      • If arr[j] ≤ pivot, increment i and swap arr[i] and arr[j].
    • Step 3.4: Swap arr[i + 1] with the pivot element arr[high].

    • Step 3.5: Return the index i + 1 as the pivotIndex.

  4. Call QuickSort(arr, 0, n-1) to sort the entire array.

  5. End

Pseudo Code for Quick Sort

QuickSort(arr, low, high):
    if low < high:
        pivotIndex = Partition(arr, low, high)
        QuickSort(arr, low, pivotIndex - 1)
        QuickSort(arr, pivotIndex + 1, high)

Partition(arr, low, high):
    pivot = arr[high]
    i = low - 1
    for j = low to high - 1:
        if arr[j] <= pivot:
            i = i + 1
            Swap(arr[i], arr[j])
    Swap(arr[i + 1], arr[high])
    return i + 1

Example Walkthrough

Array: [8, 3, 1, 7, 0, 10, 2]

  1. First Partition (Pivot = 2):

    • Rearrange elements around the pivot (2): [0, 1, 2, 7, 8, 10, 3]

    • Pivot's index = 2.

  2. Recursive Calls:

    • Left Subarray [0, 1]

    • Right Subarray [7, 8, 10, 3]

  3. Continue the process until all subarrays are sorted.


C Code for Quick Sort

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = low - 1;

    for (int j = low; j < high; j++) {
        if (arr[j] <= pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pivotIndex = partition(arr, low, high);
        quickSort(arr, low, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, high);
    }
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[] = {8, 3, 1, 7, 0, 10, 2};
    int size = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    printArray(arr, size);

    quickSort(arr, 0, size - 1);

    printf("Sorted array: ");
    printArray(arr, size);

    return 0;
}

Output of the C Code

Input: [8, 3, 1, 7, 0, 10, 2]
Output: [0, 1, 2, 3, 7, 8, 10]

Complexity Table for Quick Sort