Monday, May 29, 2023

Quick Sort: Algorithm & Implementation

 

Quick Sort: Algorithm & Implementation

1️⃣ What is Quick Sort?

🔹 Quick Sort is a divide-and-conquer sorting algorithm.
🔹 It selects a pivot element and partitions the array so that:

  • Elements smaller than pivot go to the left.
  • Elements greater than pivot go to the right.
    🔹 The process repeats recursively until the array is sorted.

✅ Time Complexity

CaseComplexity
Best Case (Balanced Partitioning)O(n log n)
Average CaseO(n log n)
Worst Case (Already Sorted, Unbalanced Partitioning)O(n²)

✅ Space Complexity

  • O(log n) (for recursive stack calls)

✅ Is it Stable?

❌ No (Relative order of equal elements may change)


2️⃣ How Quick Sort Works

🔹 Select a pivot element.
🔹 Partition the array so that:

  • Left part contains elements < pivot
  • Right part contains elements > pivot
    🔹 Recursively sort left and right partitions.

Step-by-Step Example

📝 Input: [10, 80, 30, 90, 40, 50, 70]
Pivot = 70 (can be any element, commonly last, first, or random)

StepArray StatePartitioning Around Pivot
1[10, 30, 40, 50, 70, 90, 80]Pivot 70 placed correctly
2`[10, 30, 40, 50]70
3`[10, 30]40

🔹 Final Sorted Array: [10, 30, 40, 50, 70, 80, 90]


3️⃣ Quick Sort Algorithm

🔹 Pseudocode


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++ Swap arr[i] and arr[j] Swap arr[i+1] and arr[high] return i+1

4️⃣ Quick Sort Implementation in C


#include <stdio.h> // Function to swap two elements void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // Partition function to place pivot in correct position int partition(int arr[], int low, int high) { int pivot = arr[high]; // Choosing last element as pivot 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); } // QuickSort function void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } // Function to print an array void printArray(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } // Main function int main() { int arr[] = {10, 80, 30, 90, 40, 50, 70}; int n = sizeof(arr) / sizeof(arr[0]); printf("Original array: "); printArray(arr, n); quickSort(arr, 0, n - 1); printf("Sorted array: "); printArray(arr, n); return 0; }

🔹 Output


Original array: 10 80 30 90 40 50 70 Sorted array: 10 30 40 50 70 80 90

5️⃣ Advantages & Disadvantages

✅ Advantages

✔ Faster than other O(n²) algorithms like Selection and Insertion Sort.
✔ Efficient for large datasets (O(n log n) in average case).
✔ In-place sorting (O(log n) extra space for recursion).

❌ Disadvantages

❌ Worst case O(n²) if pivot selection is poor (already sorted or reversed).
❌ Not stable (relative order of equal elements may change).
❌ Uses recursion, which may cause stack overflow for large arrays.


6️⃣ When to Use Quick Sort?

🔹 Best for large datasets (better than Bubble, Insertion, or Selection Sort).
🔹 Useful when in-place sorting is required (O(1) extra space).
🔹 Not ideal if stable sorting is required (use Merge Sort instead).

💡 For best performance, use a randomized or median pivot selection! 🚀

No comments:

Post a Comment

Complete Binary Tree in Data Structures

  Complete Binary Tree in Data Structures 🌳 A Complete Binary Tree (CBT) is a type of Binary Tree where: ✔ All levels except possibly t...