Open In App

QuickSort - Python

Last Updated : 06 Nov, 2025
Comments
Improve
Suggest changes
54 Likes
Like
Report

QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array.

Working of Quick Sort

QuickSort works on the principle of divide and conquer, breaking down the problem into smaller sub-problems. There are mainly three steps in the algorithm:

  1. Choose a Pivot: Select one element (first, last, random, or median) as the pivot.
  2. Partition the Array: Rearrange elements so that those smaller than the pivot are placed to its left and those greater are placed to its right. The pivot then moves to its correct sorted position.
  3. Recursive Sorting: Recursively apply the same process to the left and right subarrays until all elements are sorted.
  4. Base Case: When a subarray has one or no element, it is already sorted.

In this implementation, last element is chosen as the pivot, and the partitioning ensures all smaller elements are on one side and larger on the other, achieving sorting efficiently. Let's understand the working of partition algorithm with the help of the following example:

Algorithm Steps of QuickSort

1. Input: Array A, starting index low, ending index high
2. If low < high, then

  • Partition: Find pivot index p = partition(A, low, high)
  • Recur: Apply QuickSort to left subarray -> quickSort(A, low, p - 1)
    Apply QuickSort to right subarray -> quickSort(A, p + 1, high)

3. Else: Stop (base case - subarray of size ≤ 1 is already sorted)
4. Output: Sorted array A

Python Implementation

Using Recursive QuickSort Function 

This method sorts an array by selecting the last element as a pivot and partitioning the array so that smaller elements move to the left and larger ones to the right. It then recursively applies the same process to the resulting subarrays until the entire array is sorted.

Python
def partition(arr, low, high):
    pivot = arr[high]
    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

def quick_sort(arr, low, high):
    if low < high:
        p = partition(arr, low, high)
        quick_sort(arr, low, p - 1)
        quick_sort(arr, p + 1, high)

arr = [1, 7, 4, 1, 10, 9, -2]
quick_sort(arr, 0, len(arr) - 1)
print(arr)

Output
[-2, 1, 1, 4, 7, 9, 10]

Explanation:

  • partition() picks the last element as pivot and places it in its correct position.
  • Elements smaller than the pivot are swapped to the left using tuple swapping.
  • After partitioning, quick_sort() recursively sorts the left and right parts.
  • Recursion continues until all subarrays are sorted.

Using List Comprehension

This approach uses recursion and list comprehension. It selects the first element as the pivot and divides the array into two lists one for elements smaller than the pivot and one for greater elements and recursively sorts both parts.

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

arr = [1, 7, 4, 1, 10, 9, -2]
arr = quick_sort(arr)
print(arr)

Output
[-2, 1, 1, 4, 7, 9, 10]

Explanation:

  • quick_sort() selects the first element as pivot.
  • List comprehensions split elements into left (smaller) and right (greater or equal).
  • The function recursively sorts both lists.
  • Sorted parts are merged as left + pivot + right to form the final sorted array.

Explore