SwadhyAI Notes

All Python notes

Quick Sort in Python ⚡

Divide and Conquer

Quick Sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays.

How it Works

  1. Pick an element as a pivot.
  2. Partition the array around the pivot.
  3. Recursively apply the same logic to the sub-arrays.

💻 Implementation

python
def quick_sort(arr):
    if len(arr) <= 1:
        return arr
    else:
        pivot = arr.pop()
        greater = []
        lower = []
        for item in arr:
            if item > pivot:
                greater.append(item)
            else:
                lower.append(item)
        return quick_sort(lower) + [pivot] + quick_sort(greater)

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

🎮 Interactive Quick Sort Demo

Speed:
ℹ️ Ready
Comparing/Active
Sorted

⚡ Time Complexity

Average Case: O(n log n). Worst Case: O(n²) (rare, happens with bad pivot choice).