Python Tutorial

Quick Sort

A fast divide-and-conquer sort that partitions around a pivot and recurses on each side.

The Partition Strategy

Quicksort picks a pivot, then partitions the array so everything smaller than the pivot goes left and everything larger goes right. The pivot is now in its final position. It recurses on the two halves. Average time is O(n log n), and it sorts in place with low overhead — often the fastest general sort in practice.

Readable Version

def quick_sort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left  = [x for x in arr if x < pivot]
    mid   = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quick_sort(left) + mid + quick_sort(right)

print(quick_sort([3, 6, 8, 10, 1, 2, 1]))
# [1, 1, 2, 3, 6, 8, 10]

This version is easy to read but uses extra memory. The classic in-place version partitions within the array.

In-Place (Lomuto Partition)

def quick_sort_inplace(arr, low=0, high=None):
    if high is None:
        high = len(arr) - 1
    if low < high:
        p = partition(arr, low, high)
        quick_sort_inplace(arr, low, p - 1)
        quick_sort_inplace(arr, p + 1, high)
    return arr

def partition(arr, low, high):
    pivot = arr[high]               # choose last element as pivot
    i = low - 1
    for j in range(low, high):
        if arr[j] <= pivot:
            i += 1
            arr[i], arr[j] = arr[j], arr[i]
    arr[i + 1], arr[high] = arr[high], arr[i + 1]
    return i + 1                    # pivot's final index

print(quick_sort_inplace([10, 7, 8, 9, 1, 5]))
# [1, 5, 7, 8, 9, 10]

Complexity and Pivot Choice

CaseTimeWhen
Best / AverageO(n log n)Balanced partitions
WorstO(n²)Pivot is always min/max (e.g. sorted input with last-element pivot)
SpaceO(log n)Recursion stack

A fixed pivot triggers the O(n²) worst case on already-sorted data. Choose a random pivot or the median-of-three to make that case astronomically unlikely.

import random

def partition_random(arr, low, high):
    r = random.randint(low, high)
    arr[r], arr[high] = arr[high], arr[r]   # randomize pivot
    return partition(arr, low, high)

Quick Sort vs Merge Sort

  • Quicksort: in-place, cache-friendly, usually fastest — but O(n²) worst case and not stable.
  • Merge sort: guaranteed O(n log n) and stable, but needs O(n) extra memory.

Best Practices

  • Randomize or use median-of-three pivots to avoid the worst case.
  • Switch to insertion sort for small subarrays to reduce overhead.
  • Use quickselect (partition without full recursion) to find the k-th smallest in O(n) average.
  • In real code just call sorted() — CPython's Timsort is stable and highly optimized.

Try It Yourself

Exercise 1: What input triggers quicksort's O(n²) worst case with a fixed last-element pivot?

Show solution

Already-sorted (or reverse-sorted) data — every partition is maximally unbalanced. Randomizing the pivot avoids this.

Exercise 2: Is quicksort stable?

Show solution

No — partitioning can reorder equal elements. Use merge sort or Timsort when stability matters.

📘 Real-World Deep Dive

Knowing <strong>DSA Quick Sort (algorithms & data structures)</strong> well is what turns algorithms & data structures from a curiosity into a daily tool — you'll reach for it in nearly every real project.

Real-Life Scenario

An end-to-end usage of DSA Quick Sort that you'd actually see in a data pipeline or analytics notebook.

Real-Life Example

def qsort(xs):
    if len(xs) <= 1: return xs
    pivot = xs[len(xs)//2]
    less  = [x for x in xs if x <  pivot]
    equal = [x for x in xs if x == pivot]
    more  = [x for x in xs if x >  pivot]
    return qsort(less) + equal + qsort(more)

print(qsort([29, 10, 14, 37, 13, 8, 25]))

Expected Output

(see source)

Common mistakes

  • Off-by-one errors in binary-search: the standard idiom is while lo <= hi with mid = (lo + hi) // 2.
  • Recursive algorithms blow the stack for n > ~10⁴; convert to iterative with an explicit stack.
  • Comparison algorithms (sorted(iterable)) are stable by default in Python — surprising for Java/C++ users.
  • Treating DSA Quick Sort as a black box without reading the docs — the API has subtle defaults that bite when you scale.

🚀 Performance & Best Practices

  • Use bisect.bisect_left / bisect_right instead of writing your own binary search.
  • Convert a sorted search into a tuple-access pattern with numpy.searchsorted for huge arrays.
  • Use heapq for priority queues instead of maintaining a sorted list manually.
  • When working with algorithms & data structures, prefer vectorised / batched operations over Python loops.

🧪 Try It Yourself

  1. Reproduce the snippet on a representative slice of your own data.
  2. Profile the snippet with cProfile or timeit and find the single biggest improvement.
  3. Generalise the snippet into a small, reusable function you can drop into future projects.

FAQ: Quick Sort

Common questions about this page.

What is Quick Sort?

Quick Sort is a DSA lesson that explains quick sort in Python. A fast divide-and-conquer sort that partitions around a pivot and recurses on each side. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run quick sort examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn quick sort in this DSA Python lesson (Quick Sort).

How do I use quick sort in Python?

To use quick sort in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of quick sort?

This Quick Sort tutorial shows quick sort syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Quick Sort example for beginners

Yes. This page includes a beginner quick sort example you can copy and run. It is designed for searches such as "quick sort for beginners", "quick sort example", and "how to use quick sort".

What are common mistakes with quick sort?

Common quick sort mistakes include wrong syntax, mixing types, and skipping practice. Work through this DSA chapter in order, run every example, and check the output before moving on.

Why should I learn quick sort?

Quick Sort is used in real Python work. Learning quick sort helps you write clearer programs and continue the DSA tutorial on StudyGrid.

Is Quick Sort free to learn online?

Yes. You can learn quick sort free on StudyGrid (studygrid.in). This chapter is part of the DSA path and includes examples, syntax, and next-step links.