C Quick Sort

Intermediate
6 min read
Updated: Nov 2026
1 example

Partition Around a Pivot

Quick sort picks a pivot, partitions the array so smaller items come before it and larger after it, then sorts both sides recursively. The pivot lands in its final place after each partition. Average time is O(n log n); the worst case is O(n^2) when pivots are poor (for example a sorted array with a last-element pivot). It sorts in place and is not stable.

Remember
partition(lo, hi):             /* Lomuto scheme */
    pivot = a[hi]; i = lo - 1
    for j in lo..hi-1: if a[j] <= pivot: swap(a[++i], a[j])
    swap(a[i + 1], a[hi]); return i + 1

Cheat Sheet

Keep these facts handy while you work with Quick Sort in C.

TopicDetail
AverageO(n log n)
WorstO(n^2)
SpaceO(log n) stack
StableNo

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Heap Sort.

Example 1 — First partition, then full sort

The pivot is the last element, 70.

C
#include <stdio.h>

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

static int partition(int a[], int lo, int hi) {
    int pivot = a[hi], i = lo - 1;
    for (int j = lo; j < hi; j++)
        if (a[j] <= pivot) swap(&a[++i], &a[j]);
    swap(&a[i + 1], &a[hi]);
    return i + 1;
}

static void quick(int a[], int lo, int hi) {
    if (lo < hi) {
        int p = partition(a, lo, hi);
        quick(a, lo, p - 1);
        quick(a, p + 1, hi);
    }
}

static void show(const char *label, const int a[], int n) {
    printf("%s", label);
    for (int i = 0; i < n; i++) printf(" %d", a[i]);
    printf("\n");
}

int main(void) {
    int b[] = {10, 80, 30, 90, 40, 50, 70};
    int p = partition(b, 0, 6);
    show("after first partition:", b, 7);
    printf("pivot index: %d\n", p);

    int a[] = {10, 80, 30, 90, 40, 50, 70};
    quick(a, 0, 6);
    show("sorted:", a, 7);
    return 0;
}

How It Works

  1. Values 10, 30, 40, and 50 are no larger than 70, so they move to the left side.
  2. The pivot 70 is swapped into index 4, its final position.
  3. Quick sort then recurses on the left four and the right two items.

Edge Cases & Pitfalls

Check these before relying on Quick Sort in real programs.

pivot

Always picking the last element

Sorted or reverse-sorted input gives O(n^2). Use a random or median-of-three pivot.

dup

Many duplicates

Lomuto handles duplicates poorly; use a three-way partition for repeated keys.

Key Takeaways

  • Partition then recurse.
  • Pivot is placed finally.
  • Average O(n log n), worst O(n^2).

One line: Quick sort picks a pivot, partitions the array so smaller items come before it and larger after it, then sorts both sides recursively.

Frequently Asked Questions

It sorts in place with good cache locality and small constant factors, giving O(n log n) on average.
Choose the pivot randomly or by median-of-three, and recurse into the smaller side first.
Continue with "C Heap Sort" next. Build a max heap then repeatedly move the maximum to the end. The C Sorting Algorithms hub lists every topic in order.

Next: Heap Sort

Build a max heap then repeatedly move the maximum to the end.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful